Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x 获取值大于3的元素的列表列表_Python 3.x_Numpy - Fatal编程技术网

Python 3.x 获取值大于3的元素的列表列表

Python 3.x 获取值大于3的元素的列表列表,python-3.x,numpy,Python 3.x,Numpy,我有两个列表,每个列表的大小为250000。我想遍历列表并返回大于3的值 例如: import itertools from array import array import numpy as np input = (np.array([list([8,1]), list([2,3,4]), list([5,3])],dtype=object), np.array([1,0,0,0,1,1,1])) X = input[0] y = input[1] res = [ u for s in X

我有两个列表,每个列表的大小为250000。我想遍历列表并返回大于3的值

例如:

import itertools
from array import array 
import numpy as np
input = (np.array([list([8,1]), list([2,3,4]), list([5,3])],dtype=object), np.array([1,0,0,0,1,1,1]))
X = input[0]
y = input[1]
res = [ u for s in X for u in zip(y,s) ] 
res
input = [1, 3, 2, 5, 6, 7, 8, 22]
# result contains even numbers of the list 
result = filter(lambda x: x % 2 == 0, input)
我没有得到预期的输出

Actual res : [(1, 8), (0, 1), (1, 2), (0, 3), (0, 4), (1, 5), (0, 3)] 
Expected output 1 : [(8,1), (1,0), (2, 0), (3, 0), (4, 1), (5, 1), (3, 1)]
Expected output 2 : [(8,1), (4, 1), (5, 1))] ---> for greater than 3

我从stackoverflow那里得到了推荐信。也尝试了itertools。

使用
过滤器

例如:

import itertools
from array import array 
import numpy as np
input = (np.array([list([8,1]), list([2,3,4]), list([5,3])],dtype=object), np.array([1,0,0,0,1,1,1]))
X = input[0]
y = input[1]
res = [ u for s in X for u in zip(y,s) ] 
res
input = [1, 3, 2, 5, 6, 7, 8, 22]
# result contains even numbers of the list 
result = filter(lambda x: x % 2 == 0, input)
这将为您提供
result=[2,6,8,22]


不确定我完全明白你想做什么。。。但是过滤可能是一种很好的方法。

使用NumPy存储长度不一致的列表会产生很多问题,就像您看到的问题一样。如果它是一个数组整数,您只需

X[X>3]
但是,由于它是一系列列表,您必须跳过各种各样的障碍才能得到您想要的东西,并且基本上从一开始就失去了使用NumPy的所有优势。您也可以使用列表列表并完全跳过NumPy

作为替代方案,我建议使用熊猫或比NumPy更合适的东西:

将熊猫作为pd导入
df=pd.DataFrame({
“组”:[0,0,1,1,1,1,2,2],
“数据”:[8,1,2,3,4,5,4],
“标志”:[1,0,0,0,1,1,1],
})
df[df['data']>3]
#组数据标志
# 0      0     8     1
# 4      1     4     1
# 5      2     5     1
# 6      2     4     1

x%2==0
如何给出“大于3”?:)我没有在预期的输出1尝试过滤选项注意,您在这里有点滥用NumPy。。。NumPy列表数组没有提供NumPy的任何优点,因此简单的列表列表也同样适用。如果您有结构化的非方形数组数据,您可能需要查看Pandas。好的。它的元组而不是numpy数组如果我想要输出,比如:(([list([8]),list([4]),list([5]),([1,1,1])作为输入(([list([8,1]),list([2,3,4]),list([5,3]),([1,0,0,0,1,1,1])-我如何循环遍历元组并只过滤那些>3的元素,同时从第二个元组中删除相应的元素