Trying to sort values in alphabetical order for dictionary in Python
Question:
mapping = defaultdict(list)
betterMap = {}
with open ('pokemonResult.csv') as pokeMon: # Open file
reader = csv.reader(pokeMon)
next(reader)
for num,row in enumerate(reader): # Map personality as value with type as the key
mapping[row[4]].append(row[3])
for key, value in mapping.items(): # Duplicate to other dictionary
betterMap[key] = set(value)
print(sorted(betterMap.items())) # This method will sort the keys in proper order
print(sorted(betterMap.values())) # Tried to test this, but to no avail
f1 = open("pokemon4.txt", "w")
f1.write() # Will be written when the problem is sorted
f1.close()
I’m trying to read a csv file full of pokemon stats and want to make a dictionary where the keys are the pokemon types and the values are the personalities mapped to the types. The problem I’m having is that I want to sort the keys and values in alphabetical order. With the current code I have, I get this result…
[('bug', {'careful'}), ('electric', {'hardy'}), ('fairy', {'naughty', 'impish', 'sassy'}), ('fighting', {'adamant', 'serious', 'careful', 'lonely', 'hasty'}), ('fire', {'gentle', 'impish', 'bold', 'rash', 'naughty', 'lax', 'docile', 'bashful', 'modest', 'hardy', 'timid', 'jolly', 'brave'}), ('flying', {'impish'}), ('grass', {'gentle', 'impish', 'bold', 'rash', 'adamant', 'sassy', 'quiet', 'naive', 'bashful', 'docile', 'calm', 'lonely', 'mild'}), ('ground', {'gentle', 'hardy', 'bashful', 'quiet'}), ('normal', {'naughty', 'lax', 'quiet', 'serious', 'relaxed', 'jolly', 'calm', 'brave', 'mild'}), ('rock', {'impish', 'naughty', 'docile', 'bashful', 'mild'}), ('water', {'hardy'})]
The keys are sorted just how I want them, but I want the values to be sorted as well. Is there some way I can do this?
Answers:
To keep order you have to use list
instead of set()
and you have to sort every list separatelly.
But you have to remeber that dictionary
doesn’t have to keep order – in newest Python it tries to keep order but in older Python version it doesn’t have to but it has collections.OrderedDict()
for this.
data = [('bug', {'careful'}), ('electric', {'hardy'}), ('fairy', {'naughty', 'impish', 'sassy'}), ('fighting', {'adamant', 'serious', 'careful', 'lonely', 'hasty'}), ('fire', {'gentle', 'impish', 'bold', 'rash', 'naughty', 'lax', 'docile', 'bashful', 'modest', 'hardy', 'timid', 'jolly', 'brave'}), ('flying', {'impish'}), ('grass', {'gentle', 'impish', 'bold', 'rash', 'adamant', 'sassy', 'quiet', 'naive', 'bashful', 'docile', 'calm', 'lonely', 'mild'}), ('ground', {'gentle', 'hardy', 'bashful', 'quiet'}), ('normal', {'naughty', 'lax', 'quiet', 'serious', 'relaxed', 'jolly', 'calm', 'brave', 'mild'}), ('rock', {'impish', 'naughty', 'docile', 'bashful', 'mild'}), ('water', {'hardy'})]
data = dict(data)
#new_data = dict()
import collections
new_data = collections.OrderedDict()
for key in sorted(data.keys()):
new_data[key] = list(sorted(data[key]))
for item in new_data.items():
print(item)
Result:
('bug', ['careful'])
('electric', ['hardy'])
('fairy', ['impish', 'naughty', 'sassy'])
('fighting', ['adamant', 'careful', 'hasty', 'lonely', 'serious'])
('fire', ['bashful', 'bold', 'brave', 'docile', 'gentle', 'hardy', 'impish', 'jolly', 'lax', 'modest', 'naughty', 'rash', 'timid'])
('flying', ['impish'])
('grass', ['adamant', 'bashful', 'bold', 'calm', 'docile', 'gentle', 'impish', 'lonely', 'mild', 'naive', 'quiet', 'rash', 'sassy'])
('ground', ['bashful', 'gentle', 'hardy', 'quiet'])
('normal', ['brave', 'calm', 'jolly', 'lax', 'mild', 'naughty', 'quiet', 'relaxed', 'serious'])
('rock', ['bashful', 'docile', 'impish', 'mild', 'naughty'])
('water', ['hardy'])
mapping = defaultdict(list)
betterMap = {}
with open ('pokemonResult.csv') as pokeMon: # Open file
reader = csv.reader(pokeMon)
next(reader)
for num,row in enumerate(reader): # Map personality as value with type as the key
mapping[row[4]].append(row[3])
for key, value in mapping.items(): # Duplicate to other dictionary
betterMap[key] = set(value)
print(sorted(betterMap.items())) # This method will sort the keys in proper order
print(sorted(betterMap.values())) # Tried to test this, but to no avail
f1 = open("pokemon4.txt", "w")
f1.write() # Will be written when the problem is sorted
f1.close()
I’m trying to read a csv file full of pokemon stats and want to make a dictionary where the keys are the pokemon types and the values are the personalities mapped to the types. The problem I’m having is that I want to sort the keys and values in alphabetical order. With the current code I have, I get this result…
[('bug', {'careful'}), ('electric', {'hardy'}), ('fairy', {'naughty', 'impish', 'sassy'}), ('fighting', {'adamant', 'serious', 'careful', 'lonely', 'hasty'}), ('fire', {'gentle', 'impish', 'bold', 'rash', 'naughty', 'lax', 'docile', 'bashful', 'modest', 'hardy', 'timid', 'jolly', 'brave'}), ('flying', {'impish'}), ('grass', {'gentle', 'impish', 'bold', 'rash', 'adamant', 'sassy', 'quiet', 'naive', 'bashful', 'docile', 'calm', 'lonely', 'mild'}), ('ground', {'gentle', 'hardy', 'bashful', 'quiet'}), ('normal', {'naughty', 'lax', 'quiet', 'serious', 'relaxed', 'jolly', 'calm', 'brave', 'mild'}), ('rock', {'impish', 'naughty', 'docile', 'bashful', 'mild'}), ('water', {'hardy'})]
The keys are sorted just how I want them, but I want the values to be sorted as well. Is there some way I can do this?
To keep order you have to use list
instead of set()
and you have to sort every list separatelly.
But you have to remeber that dictionary
doesn’t have to keep order – in newest Python it tries to keep order but in older Python version it doesn’t have to but it has collections.OrderedDict()
for this.
data = [('bug', {'careful'}), ('electric', {'hardy'}), ('fairy', {'naughty', 'impish', 'sassy'}), ('fighting', {'adamant', 'serious', 'careful', 'lonely', 'hasty'}), ('fire', {'gentle', 'impish', 'bold', 'rash', 'naughty', 'lax', 'docile', 'bashful', 'modest', 'hardy', 'timid', 'jolly', 'brave'}), ('flying', {'impish'}), ('grass', {'gentle', 'impish', 'bold', 'rash', 'adamant', 'sassy', 'quiet', 'naive', 'bashful', 'docile', 'calm', 'lonely', 'mild'}), ('ground', {'gentle', 'hardy', 'bashful', 'quiet'}), ('normal', {'naughty', 'lax', 'quiet', 'serious', 'relaxed', 'jolly', 'calm', 'brave', 'mild'}), ('rock', {'impish', 'naughty', 'docile', 'bashful', 'mild'}), ('water', {'hardy'})]
data = dict(data)
#new_data = dict()
import collections
new_data = collections.OrderedDict()
for key in sorted(data.keys()):
new_data[key] = list(sorted(data[key]))
for item in new_data.items():
print(item)
Result:
('bug', ['careful'])
('electric', ['hardy'])
('fairy', ['impish', 'naughty', 'sassy'])
('fighting', ['adamant', 'careful', 'hasty', 'lonely', 'serious'])
('fire', ['bashful', 'bold', 'brave', 'docile', 'gentle', 'hardy', 'impish', 'jolly', 'lax', 'modest', 'naughty', 'rash', 'timid'])
('flying', ['impish'])
('grass', ['adamant', 'bashful', 'bold', 'calm', 'docile', 'gentle', 'impish', 'lonely', 'mild', 'naive', 'quiet', 'rash', 'sassy'])
('ground', ['bashful', 'gentle', 'hardy', 'quiet'])
('normal', ['brave', 'calm', 'jolly', 'lax', 'mild', 'naughty', 'quiet', 'relaxed', 'serious'])
('rock', ['bashful', 'docile', 'impish', 'mild', 'naughty'])
('water', ['hardy'])