例如,我有一个列表,比如说
list = ['sight', 'first', 'love', 'was', 'at', 'It']
我想按字长对这个列表进行分组,比如说
newlist = [['sight', 'first'],['love'], ['was'], ['at', 'It']]
请帮助我。欣赏!
使用itertools.groupby
:
>>> from itertools import groupby
>>> lis = ['sight', 'first', 'love', 'was', 'at', 'It']
>>> [list(g) for k, g in groupby(lis, key=len)]
[['sight', 'first'], ['love'], ['was'], ['at', 'It']]
请注意,为了itertools.groupby
正常工作,所有项目都必须按长度排序,否则使用collections.defaultdict
( O(N)
) 或先排序列表然后使用itertools.groupby
( O(NlogN)
)。:
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> lis = ['sight', 'first', 'foo', 'love', 'at', 'was', 'at', 'It']
>>> for x in lis:
... d[len(x)].append(x)
...
>>> d.values()
[['at', 'at', 'It'], ['foo', 'was'], ['love'], ['sight', 'first']]
如果您也希望对最终输出列表进行排序,那么最好按长度对列表项进行排序并应用于itertools.groupby
它。
您可以使用临时字典,然后按长度排序:
li=['sight', 'first', 'love', 'was', 'at', 'It']
d={}
for word in li:
d.setdefault(len(word), []).append(word)
result=[d[n] for n in sorted(d, reverse=True)]
print result
# [['sight', 'first'], ['love'], ['was'], ['at', 'It']]
您可以使用默认字典:
from collections import defaultdict
d=defaultdict(list)
for word in li:
d[len(word)].append(word)
result=[d[n] for n in sorted(d, reverse=True)]
print result
或__missing__
像这样使用:
class Dicto(dict):
def __missing__(self, key):
self[key]=[]
return self[key]
d=Dicto()
for word in li:
d[len(word)].append(word)
result=[d[n] for n in sorted(d, reverse=True)]
print result
由于groupby
已经采取了解决方案;-)
from collections import defaultdict
lt = ['sight', 'first', 'love', 'was', 'at', 'It']
d = defaultdict(list)
for x in lt:
d[len(x)].append(x)
d.values()
[['at', 'It'], ['was'], ['love'], ['sight', 'first']]