This code count the letters but i don’t want it to rewrite the same letter twice. What can i do?
d=list('banttnay')
for letter in d:
print letter, d.count(letter)
Output:
b 1
a 2
n 2
t 2
t 2
n 2
a 2
y 1
I don’t want it to rewrite ‘a 2’ or ‘t 2’ twice
use
set():or to maintain the order use
OrderedDictfromcollectionsmodule:or you can also use the
unique_everseenrecipe from theitertools Recipes: