如何对列表中的字典值进行排序?

时间:2019-05-25 23:41:11

标签: python list dictionary for-loop

我将继续进行编码练习,这使我返回了一个字典,其中的键是单词的长度,而值是单词本身。这是通过拆分文本(该文本是传递给get_word_len_dict(text)函数的参数)并计算字符数来完成的。然后将长度排序并输出到print_dict_in_key_order(a_dict)。

我得到这样的输出:

2 : ['to', 'is']
3 : ['why', 'you', 'say', 'are', 'but', 'the', 'wet']
4 : ['does', 'when', 'four', 'they', 'have']
5 : ['there', 'stars', 'check', 'paint']
7 : ['someone', 'believe', 'billion']

哪个看起来正确,但是如果我想按字母顺序对列表中的值进行排序怎么办?这意味着以大写字母开头的单词也应优先考虑。例如。 ['May','and']。

理想情况下,我希望这样的输出具有按字母顺序排列的值:

2 : ['is', 'to']
3 : ['are', 'but', 'say', 'the', 'wet', 'why', 'you']
4 : ['does', 'four', 'have', 'they', 'when']
5 : ['check', 'paint', 'stars', 'there']
7 : ['believe', 'billion', 'someone']

到目前为止,我已经设法在print_dict_in_key_order(a_dict)内对键进行了排序,但是如果我也想对值进行排序,则不确定如何处理?

def get_word_len_dict(text):
    dictionary = {}
    word_list = text.split()
    for word in word_list:
        letter = len(word)

        dictionary.setdefault(letter,[])

        if word not in dictionary[letter]:
            dictionary[letter].append(word)

    return dictionary

def test_get_word_len_dict():
    text = 'why does someone believe you when you say there are four billion stars but they have to check when you say the paint is wet'
    the_dict = get_word_len_dict(text)
    print_dict_in_key_order(the_dict)


def print_dict_in_key_order(a_dict): 
    all_keys = list(a_dict.keys()) 
    all_keys.sort() 
    for key in all_keys: 
        print(key, ":", a_dict[key]) 

3 个答案:

答案 0 :(得分:2)

鉴于此命令

d = {
    2: ['to', 'is'],
    3: ['why', 'you', 'say', 'are', 'but', 'the', 'wet'],
    4: ['does', 'when', 'four', 'they', 'have'],
    5: ['there', 'stars', 'check', 'paint'],
    7: ['someone', 'believe', 'billion'],
    }

您可以像这样对值进行排序:

{k: sorted(v) for k, v in d.items()}

输出(通过pprint):

{2: ['is', 'to'],
 3: ['are', 'but', 'say', 'the', 'wet', 'why', 'you'],
 4: ['does', 'four', 'have', 'they', 'when'],
 5: ['check', 'paint', 'stars', 'there'],
 7: ['believe', 'billion', 'someone']}

尽管如果您只关心在打印时对其进行排序,则只需在代码中更改此行即可:

print(key, ":", a_dict[key])

对此:

print(key, ":", sorted(a_dict[key]))

答案 1 :(得分:2)

您想要做的是按长度分组,然后按值排序(因为按字典顺序比较,大写字母比小写字母“小”),然后从每个组中删除重复项并将所有内容放入dict中理解力。

请注意,itertools.groupbypandas中的类似功能不同,它将不连续的组视为不同的,因此我们需要先按长度排序。

示例:

from itertools import groupby
from pprint import pprint

def solution(sentence):
    sorted_words = sorted(sentence.split(' '), key=len)
    return {length: sorted(set(words)) for length, words in groupby(sorted_words, len)}

sentence =  'Why does someone believe you when you say there are four billion stars but they have to check when you say the paint is wet'

pprint(solution(sentence))

输出:

{2: ['is', 'to'],
 3: ['Why', 'are', 'but', 'say', 'the', 'wet', 'you'],
 4: ['does', 'four', 'have', 'they', 'when'],
 5: ['check', 'paint', 'stars', 'there'],
 7: ['believe', 'billion', 'someone']}

请注意,'Why'位于其他字母之前,因为它以大写字母开头,其余字母按字母顺序排序。

如果要保留功能结构,只需对字典中的每个list进行排序:

def get_word_len_dict(text):
    dictionary = {}
    word_list = text.split()
    for word in word_list:
        letter = len(word)

        dictionary.setdefault(letter,[])

        if word not in dictionary[letter]:
            dictionary[letter].append(word)

    for words in dictionary.values():
        words.sort()

    return dictionary

答案 2 :(得分:1)

d = {
    2: ['to', 'is'],
    3: ['why', 'you', 'say', 'are', 'but', 'the', 'wet'],
    4: ['does', 'when', 'four', 'they', 'have'],
    5: ['there', 'stars', 'check', 'paint'],
    7: ['someone', 'believe', 'billion'],
    }

for i in d:
    d[i].sort()
print(d)

输出

   {
    2: ['is', 'to'],
    3: ['are', 'but', 'say', 'the', 'wet', 'why', 'you'],
    4: ['does', 'four', 'have', 'they', 'when'], 
    5: ['check', 'paint', 'stars', 'there'], 
    7: ['believe', 'billion', 'someone']
    }