Let's say I have something like this:
counts = {"chuck" : 1 , "annie" : 42, "jan" : 100}
lst = list(counts.keys())
lst.sort()
for key in lst:
print(key, counts[key])
The output would be
chuck 1
annie 42
jan 100
If I wanted to achieve this effect with a string, the string would be represented as
"chuck 1\nannie 42\njan 100\n"
Now if I wanted to accomplish this string I could try something like:
for key, value in counts.items():
if value == 1:
counts[key] = "1\n"
elif value == 42:
counts[key] = "42\n"
elif value == 100:
counts[key] = "100\n"
temp = list(counts.items())
temp.sort()
myStr = str(temp)
for char in myStr:
if char in "[(',)]":
myStr = myStr.replace(char,'')
finalStr = "" + myStr[0:19] + "" + myStr[20:37] + "" + myStr[38:53] + ""
But yeah, I think I'm overcomplicating things... What would be a better and easy way?
Thanks!