矩阵中行的交集
我在Python中有以下列表:
a = [1, 2, 3, 4 ,5]
b = [[1, 2, 3], [2, 3, 4, 5], [3, 4, 5]]
我想与每行b相交a。为此,类似的
d = set(a).intersection(set(b[0]), set(b[1]), set(b[2]))
工作将有效,但我想要迭代方法,因为如果B中的行数增加,那么像这样写所有的行都太多了。另外,如果我在A中占据许多行,则A和B的相应行还需要迭代。我们该怎么办。
我检查了交叉点,但通常它们仅给出2或3或4组的交叉点。交叉点说128套呢?
I have the following lists in python:
a = [1, 2, 3, 4 ,5]
b = [[1, 2, 3], [2, 3, 4, 5], [3, 4, 5]]
I want to intersect a with each row of b. For this something like
d = set(a).intersection(set(b[0]), set(b[1]), set(b[2]))
will work but I want iterative method because if the number of rows in b increases then writing all of them like this is too much. Also if I take many rows in a then intersecting corresponding rows of a and b would also require iteration. What do we do.
I checked intersections but usually they give the intersection of only 2 or 3 or 4 sets. What about intersection say 128 sets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以在这里做
b
,它必须是一个可迭代的,这样它就可以产生与你的系统/实现允许的一样多的项目!来自文档这里的
*
是可迭代解包运算符(文档)使用问题中定义的
a
和b
,打印d
将产生预期的输出。
希望这有帮助!如有任何疑问,请告诉我!
You can just do
b
here just has to be an iterable so it can yield as many items as your system / the implementation allows! From the documentationThe
*
here is the iterable unpacking operator (documentation) whichWith
a
andb
as defined in your question, printingd
would yield outputjust as expected.
Hope this helps! Please let me know if there are any questions!