Creating Numbered List Of Output
How can I edit my code such that my output: the 8512 and 7759 i 6182 to 6027 a 4716 of 4619 he 2873 in 2846 you 2777 was 2
Solution 1:
You'd still use enumerate()
; you didn't show how you used it but it but it solves your issue:
forindex, (value,num) in enumerate(sorted_list, start=1):
print("{}.\t{:<5}\t{:>5}".format(index, value,num))
I folded your str.ljust()
and str.rjust()
calls into the str.format()
template; this has the added advantage that it'll work for any value you can format, not just strings.
Demo:
>>>word_list = '''\...the 8512...and 7759...i 6182...to 6027...a 4716...of 4619...he 2873...in 2846...you 2777...was 2457...'''.splitlines()>>>d = dict(line.split() for line in word_list)>>>sorted_list = sorted(d.items(), key=lambda i: i[1], reverse=True)>>>for index, (value,num) inenumerate(sorted_list, start=1):...print("{}.\t{:<5}\t{:>5}".format(index, value,num))...
1. the 8512
2. and 7759
3. i 6182
4. to 6027
5. a 4716
6. of 4619
7. he 2873
8. in 2846
9. you 2777
10. was 2457
Post a Comment for "Creating Numbered List Of Output"