简体   繁体   English

尝试按字典的字母顺序对 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()

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.我正在尝试读取一个包含 pokemon 统计信息的 csv 文件,并想制作一个字典,其中键是 pokemon 类型,值是映射到类型的个性。 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.为了保持顺序,您必须使用list而不是set()并且必须分别对每个列表进行排序。

But you have to remeber that dictionary doesn't have to keep order - in newest Python it tries to keep order but in older Puthon it doesn't have to but it has collections.OrderedDict() for this.但你必须记住, 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)

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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM