尝试按 Python 中字典的字母顺序对值进行排序
Trying to sort values in alphabetical order for dictionary in Python
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()
我正在尝试读取一个充满口袋妖怪统计信息的 csv 文件,并想制作一个字典,其中键是口袋妖怪类型,值是映射到类型的个性。我遇到的问题是我想按字母顺序对键和值进行排序。使用我当前的代码,我得到了这个结果...
[('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'})]
键按照我想要的方式排序,但我希望值也按我想要的方式排序。有什么办法可以做到这一点吗?
为了保持顺序,您必须使用 list
而不是 set()
,并且您必须分别对每个列表进行排序。
但您必须记住 dictionary
不必保持秩序 - 在最新的 Python 中它会尝试保持秩序,但在较旧的 Puthon 中它不必保持秩序但它有 collections.OrderedDict()
为此。
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)
结果:
('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()
我正在尝试读取一个充满口袋妖怪统计信息的 csv 文件,并想制作一个字典,其中键是口袋妖怪类型,值是映射到类型的个性。我遇到的问题是我想按字母顺序对键和值进行排序。使用我当前的代码,我得到了这个结果...
[('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'})]
键按照我想要的方式排序,但我希望值也按我想要的方式排序。有什么办法可以做到这一点吗?
为了保持顺序,您必须使用 list
而不是 set()
,并且您必须分别对每个列表进行排序。
但您必须记住 dictionary
不必保持秩序 - 在最新的 Python 中它会尝试保持秩序,但在较旧的 Puthon 中它不必保持秩序但它有 collections.OrderedDict()
为此。
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)
结果:
('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'])