23 lines
591 B
Python
23 lines
591 B
Python
def safe_input():
|
|
while True:
|
|
try:
|
|
text = input("Введите строку")
|
|
if not text.strip():
|
|
raise ValueError
|
|
return text
|
|
except ValueError:
|
|
print("Строка не может быть пустой")
|
|
|
|
|
|
def count_chars(text):
|
|
my_list = {}
|
|
for char in text.replace(" ", ""):
|
|
my_list[char] = my_list.get(char, 0) + 1
|
|
return my_list
|
|
|
|
|
|
def get_top_tuples(my_list):
|
|
return sorted(my_list.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
print(get_top_tuples(count_chars(safe_input())))
|