save dictionary to a file without brackets.

Roman Vashkevich vashkevichrb at gmail.com
Thu Aug 9 16:41:32 EDT 2012


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
> 
> 
> 




More information about the Python-list mailing list