如何获取值对,其中第一个值取自一个列表,第二个值取自另一个列表?

发布于 2024-11-17 19:08:32 字数 149 浏览 6 评论 0原文

我想要类似下面的代码,但是采用“Pythonic”风格或使用标准库:

def combinations(a,b):
    for i in a:
        for j in b:
             yield(i,j)

I want something like the code below, but in a "Pythonic" style or using the standard library:

def combinations(a,b):
    for i in a:
        for j in b:
             yield(i,j)

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

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

发布评论

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

评论(7

玩世 2024-11-24 19:08:32

这些并不是组合学意义上的真正“组合”。这些实际上是 ab 的笛卡尔积的元素。标准库中生成这些对的函数是 itertools.product( )

for i, j in itertools.product(a, b):
    # Whatever

These are not really "combinations" in the sense of combinatorics. These are rather elements from the Cartesian product of a and b. The function in the standard library to generate these pairs is itertools.product():

for i, j in itertools.product(a, b):
    # Whatever
暮凉 2024-11-24 19:08:32

作为 Sven 说,您的代码正在尝试获取列表 ab 的所有有序元素对。在这种情况下, itertools.product(a,b) 就是您想要的。

相反,如果您实际上想要“组合”,即列表 a 中不同元素的无序对,那么您需要 itertools.combinations(a,2)

>>> for pair in itertools.combinations([1,2,3,4],2):
...    print pair
...
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

As Sven said, your code is attempting to get all ordered pairs of elements of the lists a and b. In this case itertools.product(a,b) is what you want.

If instead you actually want "combinations", which are all unordered pairs of distinct elements of the list a, then you want itertools.combinations(a,2).

>>> for pair in itertools.combinations([1,2,3,4],2):
...    print pair
...
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
清醇 2024-11-24 19:08:32

嵌套的生成器表达式也可以工作:

product = ((i, j) for i in a for j in b)
for i, j in product:
    # ...

A nested generator expression will work too:

product = ((i, j) for i in a for j in b)
for i, j in product:
    # ...
蓝天白云 2024-11-24 19:08:32

itertools 库具有组合函数。就像Sven 指出,在这种情况下,itertools.product 将是合适的函数:

list(itertools.product('ab', 'cd'))
[('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]

The itertools library has combinatorics functions. Like Sven stated, itertools.product would be the appropriate function in this case:

list(itertools.product('ab', 'cd'))
[('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]
幸福还没到 2024-11-24 19:08:32
>>>a=[1,2,3]
>>>b=[4,5,6]
>>>zip(a,b)
[(1, 4), (2, 5), (3, 6)] 
>>>a=[1,2,3]
>>>b=[4,5,6]
>>>zip(a,b)
[(1, 4), (2, 5), (3, 6)] 
绮烟 2024-11-24 19:08:32

我们可能会问的一个问题是您想要生成所有有序对还是所有无序对。 mhyfritz 的回答将为您提供所有有序对。

如果您希望所有无序对(即,(1, 2) 和(2, 1) 算作同一对),那么您需要过滤掉重复项。执行此操作的一个简单方法是将条件添加到生成器表达式的末尾,如下所示:

myList= [1, 2, 3, 4, 5]
unorderedPairGenerator = ((x, y) for x in myList for y in myList if y > x)
for pair in unorderedPairGenerator:
    print(pair)
#(1, 2)
#(1, 3)
#(1, 4)
#(1, 5)
#(2, 3)
#(2, 4)
#(2, 5)
#(3, 4)
#(3, 5)
#(4, 5)

A question we might ask is whether you want to generate all ordered pairs or all unordered pairs. The nested generator expression provided in the answer by mhyfritz will give you all ordered pairs.

If you want all unordered pairs (that is, (1, 2) and (2, 1) counts as the same pair), then you need to filter out the duplicates. An easy way to do this is to add a conditional to the end of the generator expression like so:

myList= [1, 2, 3, 4, 5]
unorderedPairGenerator = ((x, y) for x in myList for y in myList if y > x)
for pair in unorderedPairGenerator:
    print(pair)
#(1, 2)
#(1, 3)
#(1, 4)
#(1, 5)
#(2, 3)
#(2, 4)
#(2, 5)
#(3, 4)
#(3, 5)
#(4, 5)
请爱~陌生人 2024-11-24 19:08:32

创建一组对(偶数、奇数)组合

>>> a = { (i,j) for i in range(0,10,2) for j in range(1,10,2)}  
>>> a
{(4, 7), (6, 9), (0, 7), (2, 1), (8, 9), (0, 3), (2, 5), (8, 5), (4, 9), (6, 7), (2, 9), (8, 1), (6, 3), (4, 1), (4, 5), (0, 5), (2, 3), (8, 7), (6, 5), (0, 1), (2, 7), (8, 3), (6, 1), (4, 3), (0, 9)}

def combinations(lista, listb):
    return { (i,j) for i in lista for j in listb }

>>> combinations([1,3,5,6],[11,21,133,134,443])
{(1, 21), (5, 133), (5, 11), (5, 134), (6, 11), (6, 134), (1, 443), (3, 11), (6, 21), (3, 21), (1, 133), (1, 134), (5, 21), (3, 134), (5, 443), (6, 443), (1, 11), (3, 443), (6, 133), (3, 133)}

Create set of pairs (even,odd) combination

>>> a = { (i,j) for i in range(0,10,2) for j in range(1,10,2)}  
>>> a
{(4, 7), (6, 9), (0, 7), (2, 1), (8, 9), (0, 3), (2, 5), (8, 5), (4, 9), (6, 7), (2, 9), (8, 1), (6, 3), (4, 1), (4, 5), (0, 5), (2, 3), (8, 7), (6, 5), (0, 1), (2, 7), (8, 3), (6, 1), (4, 3), (0, 9)}

def combinations(lista, listb):
    return { (i,j) for i in lista for j in listb }

>>> combinations([1,3,5,6],[11,21,133,134,443])
{(1, 21), (5, 133), (5, 11), (5, 134), (6, 11), (6, 134), (1, 443), (3, 11), (6, 21), (3, 21), (1, 133), (1, 134), (5, 21), (3, 134), (5, 443), (6, 443), (1, 11), (3, 443), (6, 133), (3, 133)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文