Python:如何根据对象的特征或属性对对象列表进行分组?
问题描述:
我想将对象列表分成子列表,其中具有相同属性/特性的对象保留在同一子列表中。
I want to separate a list of objects into sublists, where objects with same attribute/characteristic stay in the same sublist.
假设我们有一个字符串列表:
Suppose we have a list of strings:
["This", "is", "a", "sentence", "of", "seven", "words"]
我们要根据字符串的长度将字符串分开,如下所示:
We want to separate the strings based on their length as follows:
[['sentence'], ['a'], ['is', 'of'], ['This'], ['seven', 'words']]
我目前想出的程序是这个
The program I currently come up with is this
sentence = ["This", "is", "a", "sentence", "of", "seven", "words"]
word_len_dict = {}
for word in sentence:
if len(word) not in word_len_dict.keys():
word_len_dict[len(word)] = [word]
else:
word_len_dict[len(word)].append(word)
print word_len_dict.values()
我想知道是否有更好的方法来实现这一目标?
I want to know if there is a better way to achieve this?
答
看看 itertools.groupby()
。请注意,您的列表必须首先排序(比方法OP 昂贵)。
>>> from itertools import groupby
>>> l = ["This", "is", "a", "sentence", "of", "seven", "words"]
>>> print [list(g[1]) for g in groupby(sorted(l, key=len), len)]
[['a'], ['is', 'of'], ['This'], ['seven', 'words'], ['sentence']]
您想要字典->
>>> {k:list(g) for k, g in groupby(sorted(l, key=len), len)}
{8: ['sentence'], 1: ['a'], 2: ['is', 'of'], 4: ['This'], 5: ['seven', 'words']}