在两个具有相同形状的列表中查找实例相似

发布于 2025-02-06 01:01:24 字数 352 浏览 1 评论 0原文

我正在使用时间表数据。假设我有两个相等形状的列表,我需要找到两个列表在同一位置的数字大于零的实例。

为了分解它,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若有似无的小暗淡 2025-02-13 01:01:25

既然您有一个 numpy tag:

A = np.array([1,0,2,0,4,6,0,5])
B = np.array([0,0,5,6,7,5,0,2])

mask = ((A>0)&(B>0))
# array([False, False,  True, False,  True,  True, False,  True])

mask.sum()
# 4

A[mask]
# array([2, 4, 6, 5])

B[mask]
# array([5, 7, 5, 2])

in Pure Python(可以推广到任何数量的列表):

A = [1,0,2,0,4,6,0,5]
B = [0,0,5,6,7,5,0,2]

mask = [all(e>0 for e in x) for x in zip(A, B)]

# [False, False, True, False, True, True, False, True]

Since you have a tag:

A = np.array([1,0,2,0,4,6,0,5])
B = np.array([0,0,5,6,7,5,0,2])

mask = ((A>0)&(B>0))
# array([False, False,  True, False,  True,  True, False,  True])

mask.sum()
# 4

A[mask]
# array([2, 4, 6, 5])

B[mask]
# array([5, 7, 5, 2])

In pure python (can be generalized to any number of lists):

A = [1,0,2,0,4,6,0,5]
B = [0,0,5,6,7,5,0,2]

mask = [all(e>0 for e in x) for x in zip(A, B)]

# [False, False, True, False, True, True, False, True]
九公里浅绿 2025-02-13 01:01:25

如果您想使用香草python,这应该是在做您想要的

l = 0
for i in range(len(A)):
    if A[i] > 0 and B[i] > 0:
        l = l + 1

If you want to use vanilla python, this should be doing what you are looking for

l = 0
for i in range(len(A)):
    if A[i] > 0 and B[i] > 0:
        l = l + 1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文