List of tuples | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

List of tuples

This is a program for voting: In the first line, the total number of votes and in the next lines the names of the countries are received as votes Finally, it will print the names of the countries and the number of votes of that country in alphabetical order and in several lines: Input: 5 Italy India India Brazil Spain Output: Brazil 1 India 2 Italy 1 Spain 1 But my output is not like this... What should i do?

31st Oct 2022, 4:16 PM
4 Antworten
+ 9
Amateur , this is your code, that is simplified for sorting and output. see also my comments.
1st Nov 2022, 3:44 PM
Lothar - avatar
+ 5
Use countries.keys() instead of countries.items() And then print dictionary according to sorted list values..
31st Oct 2022, 5:02 PM
Jayakrishna ­čç«­čç│
+ 5
You can also use comprehension to build a new, sorted dictionary based on the tuples in that sorted list ... sorted_countries = { e[ 0 ] : e[ 1 ] for e in sorted( countries.items() ) } print( sorted_countries )
31st Oct 2022, 5:50 PM
+ 2
Jayakrishna­čç«­čç│ Ipang Thank u ­čîĚ­čîĚ
31st Oct 2022, 7:19 PM