在两个具有相同形状的列表中查找实例相似
我正在使用时间表数据。假设我有两个相等形状的列表,我需要找到两个列表在同一位置的数字大于零的实例。
为了分解它,
A = [1,0,2,0,4,6,0,5]
B = [0,0,5,6,7,5,0,2]
我们可以看到在四个位置中,两个列表的数字都大于0。还有其他实例,但是我确定如果我可以获得一个简单的代码,它所有的需求就是调整符号,我也可以在更大的规模。
我已经尝试过
len([1 for i in A if i > 0 and 1 for i in B if i > 0 ])
,但我认为它给我的答案是两种情况的产物。
I am working with a timeseries data. Let's say I have two lists of equal shape and I need to find instances where both lists have numbers greater than zero at the same position.
To break it down
A = [1,0,2,0,4,6,0,5]
B = [0,0,5,6,7,5,0,2]
We can see that in four positions, both lists have numbers greater than 0. There are other instances , but I am sure if I can get a simple code, all it needs is adjusting the signs and I can also utilize in a larger scale.
I have tried
len([1 for i in A if i > 0 and 1 for i in B if i > 0 ])
But I think the answer it's giving me is a product of both instances instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然您有一个 numpy tag:
in Pure Python(可以推广到任何数量的列表):
Since you have a numpy tag:
In pure python (can be generalized to any number of lists):
如果您想使用香草python,这应该是在做您想要的
If you want to use vanilla python, this should be doing what you are looking for