save dictionary to a file without brackets.

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Aug 9 17:17:50 EDT 2012


On 09/08/2012 21:41, Roman Vashkevich wrote:
> dict.items() is a list - linear access time whereas with 'for key in dict:' access time is constant: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#use-in-where-possible-1
>
> 10.08.2012, в 0:35, Tim Chase написал(а):
>
>> On 08/09/12 15:22, Roman Vashkevich wrote:
>>>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>>>> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
>>>> 4 5 1
>>>> 5 4 1
>>>> 4 4 2
>>>> 2 3 1
>>>> 4 3 2
>>>
>>> for key in dict:
>>> 	print key[0], key[1], dict[key]
>>
>> This might read more cleanly with tuple unpacking:
>>
>>   for (edge1, edge2), cost in d.iteritems(): # or .items()
>>     print edge1, edge2, cost
>>
>> (I'm making the assumption that this is a edge/cost graph...use
>> appropriate names according to what they actually mean)
>>
>> -tkc
>>
>>
>>
>

I'm impressed, the OP gives a dict with five entries and already we're 
optimising, a cunning plan if ever there was one.  Hum, I think I'll 
start on the blast proof ferro-concrete bunker tonight just in case 
WWIII starts tomorrow.

-- 
Cheers.

Mark Lawrence.




More information about the Python-list mailing list