我有一个列表列表,它按照子列表的长度排序。例如
[[str], [str1, str2], [str1, str2], [str1, str2, str3], [str1, str2, str3],...]
我想将此列表拆分为仅包含相同长度子代的子列表。 例如
[[[str], [str], [str]], [[str1, str2], [str1, str2], [str1, str2]], ...]
我想知道下面是否有比我更有效的方法,希望代码更少。
child_list = []
new_list = []
old_list = [['e3510000'], ['e2512001'], ['e3510000'], ['e92d4010'],
['e3a0b000', 'e3a0e000'], ['e92d4030', 'e59f5054'],
['e59f3038', 'e3530000'], ['e1a0c00d', 'e92dd800']]
# length of child
length = 1
for idx, i in enumerate(old_list):
if idx == len(old_list)-1:
child_list.append(i)
new_list.append(child_list.copy())
elif length == len(i):
child_list.append(i)
elif length < len(i):
new_list.append(child_list.copy())
del child_list[:]
child_list.append(i)
length = len(i)
输出:
[[['e3510000'], ['e2512001'], ['e3510000'], ['e92d4010']],
[['e3a0b000', 'e3a0e000'], ['e92d4030', 'e59f5054'],
['e59f3038', 'e3530000'], ['e1a0c00d', 'e92dd800']]]
答案 0 :(得分:2)
您可以使用itertools.groupby
来按长度对old
中的列表进行分组。请注意,如果原始清单已按照示例中的说明按长度排序,则无需在此处进行排序。
from itertools import groupby
[list(v) for k,v in groupby(sorted(old_list, key=len), key=len)]
输出
[[['e3510000'], ['e2512001'], ['e3510000'], ['e92d4010']],
[['e3a0b000', 'e3a0e000'],
['e92d4030', 'e59f5054'],
['e59f3038', 'e3530000'],
['e1a0c00d', 'e92dd800']]]
答案 1 :(得分:2)
groupby在这种情况下可能是最直观的,但是,您可以使用字典数据结构以不同的方式解决问题:
from collections import defaultdict
data = [["str"], ["str1", "str2"], ["str1", "str2"], ["str1", "str2", "str3"], ["str1", "str2", "str3"]]
dct = defaultdict(list)
for el in data:
dct[len(el)].append(el)
print(dct.values())
出局:
[[['str']],
[['str1', 'str2'], ['str1', 'str2']],
[['str1', 'str2', 'str3'], ['str1', 'str2', 'str3']]]
基准测试结果:使用基于字典的解决方案更快:
from itertools import groupby
from collections import defaultdict
data = [["str"], ["str1", "str2"], ["str1", "str2"], ["str1", "str2", "str3"], ["str1", "str2", "str3"]]
def solve_with_groupby(data):
return [list(v) for k,v in groupby(sorted(data, key=len), key=len)]
def solve_with_dict(data):
dct = defaultdict(list)
for el in data:
dct[len(el)].append(el)
return dct.values()
结果:
In [10]: timeit solve_with_groupby(data)
100000 loops, best of 3: 5.75 µs per loop
In [11]: timeit solve_with_dict(data)
100000 loops, best of 3: 2.56 µs per loop
答案 2 :(得分:1)
只需使用groupby
:
>>> l = [[1]*i for i in range(1, 5) for _ in range(3)]
>>> l
[[1], [1], [1], [1, 1], [1, 1], [1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
>>> result = [list(g) for _, g in groupby(l, key=len)]
>>> result
[[[1], [1], [1]], [[1, 1], [1, 1], [1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]]
或者,如果没有订购,则可以使用defaultdict
:
>>> import random
>>> random.shuffle(l)
>>> l
[[1, 1, 1], [1, 1, 1, 1], [1], [1], [1, 1], [1, 1], [1, 1, 1], [1, 1, 1], [1, 1], [1], [1, 1, 1, 1], [1, 1, 1, 1]]
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for e in l:
... d[len(e)].append(e)
...
>>> result = list(d.values())
>>> result
[[[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1], [1], [1]], [[1, 1], [1, 1], [1, 1]]]