简体   繁体   English

python中的嵌套循环产生笛卡尔积

[英]Nested loop in python to produce cartesian product

I am trying to loop through two list in python to get a output like this. 我试图在python中遍历两个列表以获取这样的输出。

list1 = [a, b, c]
list2 = [1, 2, 3]

So it should loop for each value of list1 over list 2. list1[a] will loop 3 times over list2 and so on till it reaches c. 因此,它应该为列表2上list1的每个值循环。list1 [a]将在list2上循环3次,依此类推,直到达到c。 However, I am not getting the desired output. 但是,我没有得到所需的输出。

i=''
list1 = ['ABC ' + str(i)+ ' '+'1' + ' ' + '5', 'CDE '+ str(i)+ ' '+  '1'+ ' ' + '5']
list2=['123','234','456']
for c in list1:
         for i in list2:
               print c

Output: 输出:

ABC  1 5
ABC  1 5
ABC  1 5
CDE  1 5
CDE  1 5
CDE  1 5

Desired out put should be like below: 所需的输出格式如下:

ABC 123 1 5
CDE 123 1 5
ABC 234 1 5
CDE 234 1 5
ABC 456 1 5
CDE 456 1 5

Please guide me what I am missing here, am new with this. 请指导我在这里我所缺少的,这是新的。

You need to interpolate the list2 value inside the loop. 您需要在循环内插list2值。 You could the itertools.product() function instead of nesting your loops; 您可以使用itertools.product()函数,而不是嵌套循环。 do swap the order of the lists if you want the values of list1 to alterate: 如果要更改list1的值,请交换列表的顺序:

from itertools import product

list1 = ['ABC {} 1 5', 'CDE {} 1 5']
list2 = ['123','234','456']
for l2, l1 in product(list2, list1):
    print l1.format(l2)

The str.format() method fills the {} in the list1 strings with your l2 values, letting you avoid having to rebuild list2 from scratch each time: str.format()方法l2值填充list1字符串中的{} ,从而避免每次都从头开始重建list2

>>> for l2, l1 in product(list2, list1):
...     print(l1.format(l2))
...
ABC 123 1 5
CDE 123 1 5
ABC 234 1 5
CDE 234 1 5
ABC 456 1 5
CDE 456 1 5

Another alternative is for list1 to contain tuples of values to be used, and only when printing you convert everything to one string by using print() as a function (Python 3, or a from __future__ import in Python 2): 另一种选择是让list1包含要使用的值的元组 ,并且仅在打印时才通过使用print()作为函数将所有内容转换为一个字符串(Python 3或Python 2中的from __future__ import):

from __future__ import print_function

list1 = [('ABC', 1, 5), ('CDE', 1, 5)]
list2 = ['123','234','456']
for l2, (a, b, c) in product(list2, list1):
    print(a, l2, b, c)

It depends on what else you want to do with the values produced. 这取决于您还想对产生的值做些什么。

Note that if the 1 and 5 numbers are otherwise unchanged between the values of list1 , there is no need at all to do a complicated dance with string templates or tuples. 请注意,如果list1的值之间的15数字不变,则根本不需要使用字符串模板或元组进行复杂的舞蹈。 You'd just reference those inside the loop: 您只需要在循环中引用这些内容即可:

b, c = 1, 5
list1 = ['ABC', 'CDE']
list2 = ['123','234','456']
for l2, l1 in product(list2, list1):
    print '{} {} {} {}'.format(l1, l2, b, c)

I still used string formatting just to show another form of combining several values into one string. 我仍然使用字符串格式化只是为了展示将多个值组合到一个字符串中的另一种形式。

You need to move the construction of list1 into your for loop. 您需要将list1的构造移到for循环中。 Reorganize your lines as follows: 重新组织行,如下所示:

list2 = ['123', '234', '456']

for i in list2:
    list1 = ['ABC ' + str(i) + ' ' + '1' + ' ' + '5',
             'CDE ' + str(i) + ' ' + '1' + ' ' + '5']
    for c in list1:
        print c

this will print 这将打印

ABC 123 1 5
CDE 123 1 5
ABC 234 1 5
CDE 234 1 5
ABC 456 1 5
CDE 456 1 5

That being said, this can be improved using string formatting : 话虽这么说,可以使用字符串格式化来改进:

for i in list2:
    list1 = ['ABC {0} 1 5', 'CDE {0} 1 5']
    for c in list1:
        print c.format(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM