我想将对象列表分成子列表,其中具有相同属性/特征的对象保留在同一子列表中。
假设我们有一个字符串列表:
["This", "is", "a", "sentence", "of", "seven", "words"]
我们要根据字符串的长度将它们分开,如下所示:
[['sentence'], ['a'], ['is', 'of'], ['This'], ['seven', 'words']]
我目前想出的程序是这样的
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()
我想知道是否有更好的方法来实现这一目标?
看一看itertools.groupby()。请注意,您的列表必须首先排序(比方法OP更昂贵 )。
itertools.groupby()
>>> 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']}