Zho*_*Jin 6 python arrays sorting algorithm
我想将对象列表分成子列表,其中具有相同属性/特征的对象保留在同一子列表中.
假设我们有一个字符串列表:
["This", "is", "a", "sentence", "of", "seven", "words"]
Run Code Online (Sandbox Code Playgroud)
我们希望根据字符串的长度分隔字符串,如下所示:
[['sentence'], ['a'], ['is', 'of'], ['This'], ['seven', 'words']]
Run Code Online (Sandbox Code Playgroud)
我目前提出的计划是这样的
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()
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来实现这一目标?
使用defaultdict(list),您可以省略密钥存在检查:
from collections import defaultdict
word_len_dict = defaultdict(list)
for word in sentence:
word_len_dict[len(word)].append(word)
Run Code Online (Sandbox Code Playgroud)
看看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']]
Run Code Online (Sandbox Code Playgroud)
或者如果你想要一本字典 - >
>>> {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']}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7203 次 |
| 最近记录: |