簡體   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()

我正在嘗試讀取一個包含 pokemon 統計信息的 csv 文件,並想制作一個字典,其中鍵是 pokemon 類型,值是映射到類型的個性。 我遇到的問題是我想按字母順序對鍵和值進行排序。 使用我當前的代碼,我得到了這個結果......

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM