Python 如何对列表中的字典值进行排序?
我正在继续一个编码练习,它让我返回一个字典,其中键是单词的长度,值是单词本身。这是通过拆分文本来完成的,文本是传递给get_word_len_dict(text)函数的参数,并计算字符数。然后对长度进行排序,并以打印顺序(a)输出 我得到如下输出:Python 如何对列表中的字典值进行排序?,python,list,dictionary,for-loop,Python,List,Dictionary,For Loop,我正在继续一个编码练习,它让我返回一个字典,其中键是单词的长度,值是单词本身。这是通过拆分文本来完成的,文本是传递给get_word_len_dict(text)函数的参数,并计算字符数。然后对长度进行排序,并以打印顺序(a)输出 我得到如下输出: 2 : ['to', 'is'] 3 : ['why', 'you', 'say', 'are', 'but', 'the', 'wet'] 4 : ['does', 'when', 'four', 'they', 'have'] 5 : ['the
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()}
看起来不错,但是如果我想按字母顺序排列列表中的值呢?这意味着以大写字母开头的单词也应该优先考虑。例如['May','和']
理想情况下,我希望这样的输出具有按字母顺序排列的值:
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])
根据这条命令
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'],
}
可以按如下方式对值进行排序:
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
):
不过,如果您只关心打印时的排序,只需更改代码中的这一行:
print(key, ":", a_dict[key])
为此:
print(key, ":", sorted(a_dict[key]))
您要做的是按长度分组,然后按值排序(因为按字典顺序比较,大写字母比小写字母“小”),然后从每个组中删除重复的字母,并将所有内容放入
dict
理解中
请注意,itertools.groupby
,与类似的函数不同,比如说,pandas
,它会将不相邻的组视为不同的,因此我们需要首先按长度排序
例如:
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']}
请注意,“为什么”
排在其他字母之前,因为它以大写字母开头,其余字母按字母顺序排序
如果您想保留您的函数结构,只需对词典中的每个列表进行排序即可:
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: ['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']
}
对不起,我应该指定这一行,但是有没有一种方法可以在不删除代码中任何函数的情况下实现这一点?@Noneiffy04就像我写的,“如果您只关心打印时的排序,只需更改这一行”…我的错,我应该事先指定这一行,但是有没有什么方法可以在不删除任何函数的情况下实现这一点?另外,我对进口产品一点也不熟悉。很抱歉,但我希望我的三个功能保持完整。@Noneiffy04这样做很简单;检查我编辑的答案。只需添加两行即可。