+ 4
sort two lists accordingly
So I have recentely made a text analyser, but I want the output to be alphabetical without changing the order of list1. So does anyone know a way to sort the output of this code? Here follows the code text=input() list1=["e", "t", "a", "o", "i", "n", "s", "h", "r", "d", "l", "c", "u", "m", "w", "f", "g", "y", "p", "b", "v", "k", "j", "x", "q", "z"] list2=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] for char in (text): for i in range(26): if char==list1[i]: list2[i]+=1 break continue for i in range(26): if list2[i]!=0: print("ammount of "+str(list1[i])+"\'s: "+str(list2[i]))
3 Answers
+ 5
I tried to make code that does not use a dict.
for alphabet in [chr(i) for i in range(97,97+26)]:
x = list1.index(alphabet)
if list2[x] != 0:
print("ammount of " + list1[x] + "\'s: " + str(list2[x]))
+ 4
text = input()
list1 = ["e", "t", "a", "o", "i", "n", "s", "h", "r", "d", "l", "c", "u", "m", "w", "f", "g", "y", "p", "b", "v", "k", "j", "x", "q", "z"]
list2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
for char in (text):
for i in range(26):
if char == list1[i]:
list2[i]+=1
break
continue
d = dict(zip(list1, list2))
for alphabet in [chr(i) for i in range(97,97+26)]:
if d[alphabet] != 0:
print("ammount of " + alphabet + "\'s: " + str(d[alphabet]))
+ 2
I think it is better to use dictionary. Then the code can be changed like following:
text=input()
dict={}
for char in text:
if char in dict.keys():
dict[char]+=1
else:
dict[char]=1
for x in range(ord('a'),ord('z')+1):
if chr(x) in dict.keys():
print("amount of "+chr(x)+"\'s: "+str(dict[chr(x)]))
I'm sorry I use python 2. I hope it works on 3 too..
It will show the number of characters in alphabetical order