Python中的快速子集子列表

时间:2017-02-20 09:33:13

标签: python list loops subset

我有一份包含数千条记录的子列表。 列表中的示例如下:

additionalClasspathElement

我想将这个主列表分成两个单独的列表。第一个列表应该具有来自子列表的前两个记录,第二个列表应该具有剩余的记录。 例如,我需要将它们存储为:

list_full = [
    [343, 354,"Sometext1", "Sometext2", "Sometext3"],
    [43543, 345435,"Sometext1", "Sometext2", "Sometext3"],
    [4354, 54354,"Sometext1", "Sometext2", "Sometext3",
         "Sometext4", "Sometext5"]
]

我有以下脚本:

list_a = [[343, 354], [43543, 345435], [004354, 54354]]
list_b = [["Sometext1", "Sometext2", "Sometext3"], ["Sometext1", "Sometext2", "Sometext3"], ["Sometext2", "Sometext3", "Sometext4", "Sometext5"]]

但是使用循环并且有点慢。有没有更快的方法来执行此列表子集?谢谢!

3 个答案:

答案 0 :(得分:1)

我会懒惰地使用itertools

import itertools

list1 = itertools.imap(lambda x: itertools.islice(x, 0, 2), list_full)
list1 = itertools.imap(lambda x: itertools.islice(x, 2, None), list_full)

这样,在您需要计算之前,不会对计算进行评估。请注意,如果您希望立即评估它,可以调用list()将itertools对象转换为列表。

答案 1 :(得分:1)

试试这个:

# to create two lists
list_1 = [x[:2] for x in list_full]
list_2 = [x[2:] for x in list_full]

# to create two generators, supporting iteration (with for) but not indexing
list_1 = (x[:2] for x in list_full)
list_2 = (x[2:] for x in list_full)

或者,在python 3中,轻松迭代数据而不创建列表:

for a, b, *other in list_full:
    print("first:", a, b)
    print("other:", other)

答案 2 :(得分:1)

您可以尝试列表理解,

list_1=[item[:2] for item in list_full]
list_2 = [item[2:] for item in list_full]

基于某些documents列表理解比循环快得多。

您可以使用timeit

计算方法所花费的时间
from timeit import default_timer as timer
start=timer()
list_1 = []
list_2 = []

for item in list_full:
    list_1.append(item[:2])
    list_2.append(item[2:])

print list_1
print list_2

end=timer()

print end-start #Takes 0.0002339

使用列表理解时,

from timeit import default_timer as timer
start = timer()
list_1=[item[:2] for item in list_full]
list_2 = [item[2:] for item in list_full]

print list_1
print list_2

end = timer()

print end-start #gives 0.0002105