取图'&&'任意数量的列表

发布于 2025-02-03 07:09:06 字数 412 浏览 3 评论 0原文

我有一个任意数量的列表,我想参加布尔值&。例如,对于2个列表,我有

x = [0, 1, 0, 0]
y = [1, 1, 0, 1]

[np.array(x) & np.array(y) for x,y in zip(x, y)]
[0, 1, 0, 0]

3个列表,

z=[0,1,1,1]

我将拥有

[np.array(x) & np.array(y) & np.array(y) for x,y,z in zip(x, y, z)]
[0, 1, 0, 0]

等。

因为我有执行此操作的任意数量的列表,所以实现此目的的最佳方法是什么?

I have an arbitrary number of lists that I want to take the boolean & of. For example for 2 lists, I have

x = [0, 1, 0, 0]
y = [1, 1, 0, 1]

[np.array(x) & np.array(y) for x,y in zip(x, y)]
[0, 1, 0, 0]

for 3 lists

z=[0,1,1,1]

I would have

[np.array(x) & np.array(y) & np.array(y) for x,y,z in zip(x, y, z)]
[0, 1, 0, 0]

etc.,

since my I have an arbitrary number of lists over which to perform this operation, what is the best method to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

も星光 2025-02-10 07:09:06

您可以使用zipall

x = [0,1,0,0]
y = [1,1,0,1]
z = [0,1,1,1]

output = [all(zipped) for zipped in zip(x, y, z)]
print(output) # [False, True, False, False]

如果您想获得[0,1,0,0]而不是int (所有(zpight))而不是所有(zpipped)(但是在大多数情况下,此明确的重铸是多余的)。

You can use zip and all:

x = [0,1,0,0]
y = [1,1,0,1]
z = [0,1,1,1]

output = [all(zipped) for zipped in zip(x, y, z)]
print(output) # [False, True, False, False]

If you do want to get [0,1,0,0] instead, use int(all(zipped)) instead of all(zipped) (but this explicit recasting is redundant in most cases).

⒈起吃苦の倖褔 2025-02-10 07:09:06

或使用np.logical_and

x = [0, 1, 0, 1]
y = [1, 1, 0, 1]
z = [1, 1, 0, 1]

np.logical_and(x, y, z).astype(int)
array([0, 1, 0, 1])

or using np.logical_and

x = [0, 1, 0, 1]
y = [1, 1, 0, 1]
z = [1, 1, 0, 1]

np.logical_and(x, y, z).astype(int)
array([0, 1, 0, 1])
乄_柒ぐ汐 2025-02-10 07:09:06

With map and min:

x = [0,1,0,0]
y = [1,1,0,1]
z = [0,1,1,1]

output = list(map(min, x, y, z))
print(output)

Output (在线尝试!):

[0, 1, 0, 0]

nofollow noreferrer“> wikipedia” /最大和/或。

如果您有列表的 list (如“任意号码”所建议,因为您不想使用任意数量的变量),则可以使用list(map,min, * code> list列表))

With map and min:

x = [0,1,0,0]
y = [1,1,0,1]
z = [0,1,1,1]

output = list(map(min, x, y, z))
print(output)

Output (Try it online!):

[0, 1, 0, 0]

Wikipedia btw even mentions using min/max for and/or.

If you have a list of lists (as "arbitrary number" suggests, since you wouldn't want to use an arbitrary number of variables), you can use list(map(min, *lists)).

谁许谁一生繁华 2025-02-10 07:09:06

10表现得像True和False

  lists=[x,y,z]
  np.prod(lists,axis=0)

1 and 0 act like True and False so using product works

  lists=[x,y,z]
  np.prod(lists,axis=0)
两相知 2025-02-10 07:09:06
import numpy as np
import functools
import operator

x = np.array([0, 1, 0, 0])
y = np.array([1, 1, 0, 1])
z = np.array([0, 1, 1, 1])

print(functools.reduce(operator.and_, [x, y, z]))
import numpy as np
import functools
import operator

x = np.array([0, 1, 0, 0])
y = np.array([1, 1, 0, 1])
z = np.array([0, 1, 1, 1])

print(functools.reduce(operator.and_, [x, y, z]))
抚你发端 2025-02-10 07:09:06

由于您正在使用numpy,因此可以使用:

np.bitwise_and.reduce(np.array([x,y,z]))

示例:

>>> x = [0, 1, 0, 0]
>>> y = [1, 1, 0, 1]
>>> z = [0, 1, 1, 1]
>>> np.bitwise_and.reduce([x, y, z])
array([0, 1, 0, 0])

但是我不会为此使用numpy 。我可能会选择J1-Lee的答案。

只是为了娱乐,这是一个答案的另一个版本:

>>> from functools import partial, reduce
>>> import operator as op
>>> list(map(partial(reduce, op.and_), zip(x,y,z)))
[0, 1, 0, 0]
>>>

因为您是映射zipped数据的减少操作

Since you are using numpy anyway, you can use:

np.bitwise_and.reduce(np.array([x,y,z]))

Example:

>>> x = [0, 1, 0, 0]
>>> y = [1, 1, 0, 1]
>>> z = [0, 1, 1, 1]
>>> np.bitwise_and.reduce([x, y, z])
array([0, 1, 0, 0])

But I wouldn't use numpy just for this. I'd probably go with j1-lee's answer.

Just for fun, here is another version of that answer:

>>> from functools import partial, reduce
>>> import operator as op
>>> list(map(partial(reduce, op.and_), zip(x,y,z)))
[0, 1, 0, 0]
>>>

because you are mapping a reduction operation over the zipped data

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