Python. file in, two files out, sorting by key and value of a dictionary. Read from a file input.txt: a number and a name each on a line (the file could be of any number of lines, here is 6 as an example below) 4 Cows 5 Camels 1 Peacock 3 Whales 4 Lions 2 Scorpions create two output files: first output_file1.txt : sort by (dictionary) key but those sharing the same key should be listed on the same line with ; separation 1: Peacock 2: Scorpions 3: Whales 4: Cows; lions ------------------------------> separated by ';' because they share the same key 4 5: Camels second output_file2.txt: Only values of the dictionary are sorted as follows and written to file: Camels Cows Lions Peacock Scorpions Whales
Python. file in, two files out, sorting by key and value of a dictionary.
Read from a file input.txt:
a number and a name each on a line (the file could be of any number of lines, here is 6 as an example below)
4
Cows
5
Camels
1
Peacock
3
Whales
4
Lions
2
Scorpions
create two output files:
first output_file1.txt : sort by (dictionary) key but those sharing the same key should be listed on the same line with ; separation
1: Peacock
2: Scorpions
3: Whales
4: Cows; lions ------------------------------> separated by ';' because they share the same key 4
5: Camels
second output_file2.txt:
Only values of the dictionary are sorted as follows and written to file:
Camels
Cows
Lions
Peacock
Scorpions
Whales
Thank you
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images