I have these single-line dictionaries which is read from standard input.
def gen_with appropriate_name():
for n, line in enumerate(sys.stdin):
d = ast.literal_eval(line)
items = d.values()[0].items()
items.sort(key = lambda itm: itm[0])
yield {n+1: {i+1:item[1] for i, item in enumerate(items)}}
d = gen_with appropriate_name() # prime the generator
d.next()
for thing in d:
print thing
If you print 'd' then I get the o/p as the dictionary as I showed below.
{ 1 : {'1': 5, '2': 6, '3': 0} }
{ 2 : {'1': 6, '2': 4, '3': 0} }
{ 3 : {'1': 2, '2': 9, '3': 1} }
I want to convert them to this:
1 1: 5 2: 6 3: 0
2 1: 6 2: 4 3: 0
3 1: 2 2: 9 3: 1
Since dictionary does not have a replace or re.sub method. Its becoming complicated to format them. Also, I cannot convert dict to a string and then do formatting.
for item in [str(k) + " " + "".join(str(k1) + ": " + str(v1) + " " for k1, v1 in v.items()) for k, v in d.items()]:
print item