4

例如,我有一个列表,比如说

list = ['sight', 'first', 'love', 'was', 'at', 'It']

我想按字长对这个列表进行分组,比如说

newlist = [['sight', 'first'],['love'], ['was'], ['at', 'It']]

请帮助我。欣赏!

4

3 回答 3

9

使用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它。

于 2013-11-08T20:38:15.893 回答
5

您可以使用临时字典,然后按长度排序:

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
于 2013-11-08T20:43:28.460 回答
2

由于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']]
于 2013-11-08T20:43:44.360 回答