[Tutor] help on dic creation

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Nov 6 15:48:41 CET 2012


On 06/11/2012 14:16, Dave Angel wrote:
> On 11/06/2012 09:01 AM, Mark Lawrence wrote:
>> On 31/10/2012 01:01, Brayden Zhao wrote:
>>
>>> def fieldict(filename):
>>>     D={}
>>>     with open(filename) as FileObject:
>>>       for lines in FileObject:
>>>         linelist=lines.split('\t')
>>>         Key=linelist[0]
>>>         ValCity=(linelist[12]).strip()
>>>         ValState=linelist[13]
>>>         ValOne=linelist[2]
>>>         ValTwo=linelist[6]
>>>         ValThree=boolean(linelist[7])
>>>     D={Key:(ValOne, ValTwo, ValThree, ValCity,ValState)}
>>
>> Put the line above inside the for loop :)
>>
> Won't help.  Then he'd be reassigning the D each time, with the same net
> result.  What he needs INSIDE the loop is something like:
>
>      D[Key] = (ValOne, ValTwo, ValThree, ValCity, ValState)
>
>>>     return D
>>> print fieldict("DOT500.txt")
>>>
>>
>
>

Thanks for the correction, that'll teach me to pay attention :(

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list