save dictionary to a file without brackets.

Tim Chase python.list at tim.thechases.com
Thu Aug 9 16:35:32 EDT 2012


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