Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查值或值列表是否是python中列表的子集的最快方法_Python_List_Numpy_Cython - Fatal编程技术网

检查值或值列表是否是python中列表的子集的最快方法

检查值或值列表是否是python中列表的子集的最快方法,python,list,numpy,cython,Python,List,Numpy,Cython,我有一个非常大的列表名为main\u list,包含大约1300万个列表,每个列表包含6个数字。我正在寻找一种方法来过滤掉任何不包含特定值的列表。例如,要创建仅包含值为4和5的列表的新列表,我的代码如下所示: and_include = [] temp_list=[4,5] for sett in main_list: if set(temp_list).issubset(sett): and_include.append(sett) 这需要大约5秒钟的时间来运行,这对

我有一个非常大的列表名为
main\u list
,包含大约1300万个列表,每个列表包含6个数字。我正在寻找一种方法来过滤掉任何不包含特定值的列表。例如,要创建仅包含值为4和5的列表的新列表,我的代码如下所示:

and_include = []
temp_list=[4,5]
for sett in main_list:
    if set(temp_list).issubset(sett):
        and_include.append(sett)
这需要大约5秒钟的时间来运行,这对于频繁使用可能会很烦人,所以我想知道是否有更快的方法来完成这项工作,使用numpy或cython

我对cython不是很熟悉,但我试着用这种方式实现,编译了它,但我遇到了一个错误

def andinclude(list main_list,list temp_list):
    and_include=[]
    for sett in main_list:
        if set(temp_list).issubset(sett):
            and_include.append(sett)
    return and_include

希望有一种更快的方法?

您可以使用列表理解而不是在循环中追加。此外,您可能希望将
set(temp\u list)
的结果存储在局部变量中,这样您就不会为相同的结果调用
set
1300万次。

以下是
numpy
解决方案:

import numpy as np

# Randomly generate 2d array of integers
np.random.seed(1)
a = np.random.randint(low=0, high=9, size=(13000000, 6))

# Use numpy indexing to filter rows
results = a[(a == 4).any(axis=1) & (a == 5).any(axis=1)]
结果:

In [35]: print(results.shape)
(3053198, 6)

In [36]: print(results[:5])
[[5 5 4 5 5 1]
 [5 5 4 3 8 6]
 [2 5 8 1 1 4]
 [0 5 4 1 1 5]
 [3 2 5 2 4 6]]
In [37]: %timeit results = a[(a == 4).any(axis=1) & (a == 5).any(axis=1)]
923 ms ± 38.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
计时:

In [35]: print(results.shape)
(3053198, 6)

In [36]: print(results[:5])
[[5 5 4 5 5 1]
 [5 5 4 3 8 6]
 [2 5 8 1 1 4]
 [0 5 4 1 1 5]
 [3 2 5 2 4 6]]
In [37]: %timeit results = a[(a == 4).any(axis=1) & (a == 5).any(axis=1)]
923 ms ± 38.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
如果需要将结果转换回列表列表而不是二维numpy数组,可以使用:

l = results.tolist()

这使在我的机器上运行的时间增加了大约50%,但应该比任何涉及在Python列表上循环的解决方案都要快。

如果他得到了列表,将所有内容转换为numpy,然后再将其转换回列表不是更慢吗?输入是列表而不是numpy数组(但我非常同意你的观点,为了性能,列表应该是无效的)@BlueSheepToken这是一个有效的观点,谢谢。如果OP被迫以所描述的列表列表开始,那么我的解决方案是不好的。我希望OP能够将他们的数据集(无论它来自哪里)直接导入到NumPy数组中,但这可能是不可能的。在决定是否保留/删除/编辑我的答案之前,我将等待并查看OP是否有评论。