Sketched such a code

from collections import Counter counter = Counter() with open('work.txt') as file: for line in file: if line.strip(): # skip blank lines name, money = line.split() counter[name] = int(int(money)*0.25+(int(money))) for name, money in counter.most_common(): print(name, money) 

It collects employee data from a file. Here's what's in the file:

 Лужков 10000 Измайлов 11000 Васьков 13000 Дмитриев 12000 Гуськов 7000 

Increases money value by 25% and displays. All is well. but how to sort alphabetically?

  • aside: counter[name] = int(money)*5//4 - jfs

1 answer 1

It's not entirely clear why you are using Counter . For data type key-value dictionary is used.

 result_dict = {} with open('work.txt') as file: for line in file: if line.strip(): name, money = line.split() result_dict[name] = int(money) * 1.25 for name, money in sorted(result_dict.items()): print(name, money) 

The items method returns a dictionary view as a set of tuples (key, value) . Since the sorted function sorted called without a sort parameter, the default behavior is used: sorting by the first element of the tuple — the last name in this case.

  • Oh, lucidly. Thank you) - RacCoon