[Tutor] updating a dictionary

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Feb 20 08:17:31 CET 2015


On 20/02/2015 00:55, Chris Stinemetz wrote:
>>
>>
>>
>>
>> Other than the minor tweaks I've suggested I'm not sure what your problem
>> is? I think we need to see the data to understand the issue.
>
>
> ​Here is a sample of the input data, it is tab delimited and I chopped it
> down for example purposes:
>>
>   KSL03502_7A_1 11.5921
> KSL03502_7B_1 46.4997
> KSL03502_7C_1 13.5839
> KSL03505_7A_1 12.8684
> KSL03505_7B_1 16.5311
> KSL03505_7C_1 18.9926
> KSL03509_7A_1 3.4104
> KSL03509_7B_1 40.6244
> KSL03509_7C_1 51.0597
> KSL03511_7A_1 7.128
> KSL03511_7B_1 53.4401
> KSL03511_7C_1 66.2584
> KSL03514_2A_1 25.6476
> KSL03514_2B_1 53.17
> KSL03514_2C_1 11.6469
> KSL03514_7A_1 39.2292
> KSL03514_7B_1 65.675
> KSL03514_7C_1 3.4937
>
>
> ​I would like to parse it buy using a dictionary structure. Where each row
> would be something like:
>
> name 7,8,9,2
> KSL03514_C,3.4937,,,11.6469
> KSL03514_B,65.675,,,53.17
>
> I am just showing an example of what KSL03514_7C_1, KSL03514_2C_1,
> KSL03514_7B_1, KSL03514_2B_1 would parse.
>
> Hope this helps explain what I am trying to accomplish.
>
> Thank you in advance.
>

I think you need a defaultdict rather than a plain dict, otherwise 
you'll always be overwriting your values with new data.  I believe this 
https://docs.python.org/3/library/collections.html#defaultdict-examples
is exactly what you need.  Am I close?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list