在python中使用两个for循环

时间:2023-01-18 20:00:29

I have started to learn python recently and have a question about for loops that I was hoping someone could answer. I want to be able to print all the possible products of two numbers from one to ten. so: 2 by 2, 2 by 3, 2 by 4...2 by 10, 3 by 2, 3 by 3...3 by 10, 4 by 2, 4 by 3 etc...I would have thought the easiest way to do so would be to use two for loops but I am not sure. could anyone please tell me how this is done. thanks very much. asadm.

我最近开始学习python并且有一个关于for循环的问题,我希望有人可以回答。我希望能够打印从1到10的两个数字的所有可能产品。所以:2乘2,2乘3,2乘4 ...... 2乘10,3乘2,3乘3 ...... 3乘10,4乘2,4乘3等......我原以为最简单的方法是使用两个for循环,但我不确定。谁能告诉我这是怎么做的。非常感谢。 asadm。

4 个解决方案

#1


8  

Here is another way

这是另一种方式

a = [i*j for i in xrange(1,11) for j in xrange(i,11)]

note we need to start second iterator from 'i' instead of 1, so this is doubly efficient

注意我们需要从'i'而不是1开始第二个迭代器,所以这是双重效率

edit: proof that it is same as simple solution

编辑:证明它与简单的解决方案相同

b = []
for i in range(1,11):
    for j in range(1,11):
        b.append(i*j)

print set(a) == set(b)

#2


5  

Just for fun (and the itertools-addicted SO readers) using only one for-loop:

只是为了好玩(和itertools上瘾的SO读者)只使用一个for循环:

from itertools import product
for i,j in product(xrange(1,11), xrange(1,11)):
    print i*j

EDIT: using xrange as suggested by Hank Gay

编辑:使用Hank Gay建议的xrange

#3


4  

for i in range(1, 11):
    for j in range(1, 11):
        print i * j

#4


0  

You may not need the nested for-loop Solution.
A Single Loop with List Comprehension (as shown below) would suffice:

您可能不需要嵌套的for循环解决方案。具有列表理解的单循环(如下所示)就足够了:

r_list  = list(range(2, 11))   
output  = []
for m in r_list:
    tmp = [m*z for z in r_list]
    output.append(tmp)

print(output)

Or Simpler:

或者更简单:

output  = []
for m in list(range(2, 11)):
    tmp = [m*z for z in list(range(2, 11))]
    output.append(tmp)

print(output)

Prints:

打印:

    [
        [4, 6, 8, 10, 12, 14, 16, 18, 20], 
        [6, 9, 12, 15, 18, 21, 24, 27, 30], 
        [8, 12, 16, 20, 24, 28, 32, 36, 40], 
        [10, 15, 20, 25, 30, 35, 40, 45, 50], 
        [12, 18, 24, 30, 36, 42, 48, 54, 60], 
        [14, 21, 28, 35, 42, 49, 56, 63, 70], 
        [16, 24, 32, 40, 48, 56, 64, 72, 80], 
        [18, 27, 36, 45, 54, 63, 72, 81, 90], 
        [20, 30, 40, 50, 60, 70, 80, 90, 100]
    ]

#1


8  

Here is another way

这是另一种方式

a = [i*j for i in xrange(1,11) for j in xrange(i,11)]

note we need to start second iterator from 'i' instead of 1, so this is doubly efficient

注意我们需要从'i'而不是1开始第二个迭代器,所以这是双重效率

edit: proof that it is same as simple solution

编辑:证明它与简单的解决方案相同

b = []
for i in range(1,11):
    for j in range(1,11):
        b.append(i*j)

print set(a) == set(b)

#2


5  

Just for fun (and the itertools-addicted SO readers) using only one for-loop:

只是为了好玩(和itertools上瘾的SO读者)只使用一个for循环:

from itertools import product
for i,j in product(xrange(1,11), xrange(1,11)):
    print i*j

EDIT: using xrange as suggested by Hank Gay

编辑:使用Hank Gay建议的xrange

#3


4  

for i in range(1, 11):
    for j in range(1, 11):
        print i * j

#4


0  

You may not need the nested for-loop Solution.
A Single Loop with List Comprehension (as shown below) would suffice:

您可能不需要嵌套的for循环解决方案。具有列表理解的单循环(如下所示)就足够了:

r_list  = list(range(2, 11))   
output  = []
for m in r_list:
    tmp = [m*z for z in r_list]
    output.append(tmp)

print(output)

Or Simpler:

或者更简单:

output  = []
for m in list(range(2, 11)):
    tmp = [m*z for z in list(range(2, 11))]
    output.append(tmp)

print(output)

Prints:

打印:

    [
        [4, 6, 8, 10, 12, 14, 16, 18, 20], 
        [6, 9, 12, 15, 18, 21, 24, 27, 30], 
        [8, 12, 16, 20, 24, 28, 32, 36, 40], 
        [10, 15, 20, 25, 30, 35, 40, 45, 50], 
        [12, 18, 24, 30, 36, 42, 48, 54, 60], 
        [14, 21, 28, 35, 42, 49, 56, 63, 70], 
        [16, 24, 32, 40, 48, 56, 64, 72, 80], 
        [18, 27, 36, 45, 54, 63, 72, 81, 90], 
        [20, 30, 40, 50, 60, 70, 80, 90, 100]
    ]