Поэтому я хочу, чтобы мой код выводил каждую букву в строке только один раз и в алфавитном порядке, например, banana
будет выводить abn
...
Суть в том, что мне все еще нужно это посчитайте вхождения каждой буквы в строке, поэтому выходные данные должны быть следующими:
a occurs in the word banana a total of 3 times(s)
b occurs in the word banana a total of 1 time(s)
n occurs in the word banana a total of 2 time(s)
...
Это мой код:
def letter_counter(string):
stg = string.lower()
stg = ''.join(sorted(stg))
for i in stg:
a = stg.count(i)
print(f'the letter {i} appears in the word {string} {a} times')
letter_counter('banana')
А текущий вывод выглядит следующим образом:
the letter a appears in the word banana 3 times
the letter a appears in the word banana 3 times
the letter a appears in the word banana 3 times
the letter b appears in the word banana 1 times
the letter n appears in the word banana 2 times
the letter n appears in the word banana 2 times