取图'&&'任意数量的列表
我有一个任意数量的列表,我想参加布尔值&
。例如,对于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
zip
和all
:如果您想获得
[0,1,0,0]
而不是int (所有(zpight))
而不是所有(zpipped)
(但是在大多数情况下,此明确的重铸是多余的)。You can use
zip
andall
:If you do want to get
[0,1,0,0]
instead, useint(all(zipped))
instead ofall(zipped)
(but this explicit recasting is redundant in most cases).或使用np.logical_and
or using np.logical_and
With map and min:
Output (在线尝试!):
nofollow noreferrer“> wikipedia” /最大和/或。
如果您有列表的 list (如“任意号码”所建议,因为您不想使用任意数量的变量),则可以使用
list(map,min, * code> list列表))
。With map and min:
Output (Try it online!):
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))
.1
和0
表现得像True和False1
and0
act like True and False so usingproduct
works由于您正在使用
numpy
,因此可以使用:示例:
但是我不会为此使用numpy 。我可能会选择J1-Lee的答案。
只是为了娱乐,这是一个答案的另一个版本:
因为您是映射zipped数据的减少操作
Since you are using
numpy
anyway, you can use:Example:
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:
because you are mapping a reduction operation over the zipped data