python - Pack dictionary with values separated by comma -



python - Pack dictionary with values separated by comma -

i have text file this:

key1 value1 key1 value2 b key1 value3 key2 value1 key2 value2 b

i trying open dictionary , print list of keys , values separated commas looks in end:

key1 value1,value2,value a,b,a key2 value1,value2,value a,b

i trying next code:

f = open('file.txt', 'r') reply = {} line in f: list = ",".join(map(str,f)) print list

but it's not working

any ideas doing wrong? help

using collections.defaultdict:

from collections import defaultdict open('file.txt') f: reply = defaultdict(lambda: ([], [])) line in f: key, value, alpha = line.split() answer[key][0].append(value) answer[key][1].append(alpha) key, (values, alphas) in sorted(answer.items()): print key, ','.join(values), ','.join(alphas)

output:

key1 value1,value2,value3 a,b,a key2 value1,value2 a,b

python list python-2.7 dictionary

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -