嵌套循环 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24591917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:50:32  来源:igfitidea点击:

Nested Loop Python

pythonpython-3.xnested-loops

提问by iMaxPrime

count = 1
for i in range(10):
    for j in range(0, i):
        print(count, end='')
        count = count +1
    print()
input()

I am writing a program that should have the output that looks like this.

我正在编写一个程序,它应该具有如下所示的输出。

1

22

333

4444

55555

666666

7777777

88888888

999999999   

With the above code I am pretty close, but the way my count is working it just literally counts up and up. I just need help getting it to only count to 9 but display like above. Thanks.

使用上面的代码,我非常接近,但我的计数工作方式只是字面上计数。我只需要帮助让它只数到 9,但显示如上。谢谢。

enter image description here

在此处输入图片说明

采纳答案by Dannnno

You're incrementing countin the inner loop which is why you keep getting larger numbers before you want to

count在内循环中递增,这就是为什么您在想要之前不断获得更大的数字

You could just do this.

你可以这样做。

>>> for i in range(1, 10):
        print str(i) * i


1
22
333
4444
55555
666666
7777777
88888888
999999999

or if you want the nested loop for some reason

或者如果您出于某种原因想要嵌套循环

from __future__ import print_function

for i in range(1, 10):
    for j in range(i):
        print(i, end='')
    print()

回答by David Ehrmann

Change print(count, end='')to print(i + 1, end='')and remove count. Just make sure you understand why it works.

更改print(count, end='')print(i + 1, end='')并删除count. 只要确保您了解它的工作原理。

回答by darkcharl

This works in both python2 and python3:

这适用于 python2 和 python3:

for i in range(10):
  print(str(i) * i)

回答by Zacrath

Is this what you want:

这是你想要的吗:

for i in range(10):
    print(str(i) * i)

回答by Harit Singh

"""2. 111 222 333 printing"""

for l in range (1,10):
    for k in range(l):
        print(l,end='')
print()

回答by Raghav Chadha

for i in range(1,10):
    for j in range(0,i):
        print i,
print "\n"

回答by Fredrick Kofi Tam

What you are trying to do involves a mathematical concept called repunit numbers

你正在尝试做的涉及一个叫做重复数的数学概念

you could also do it as follows:

你也可以这样做:

for i in range(1,n):
    print (int(i*((10**i)-1)/9))

回答by noobcoder

Others have suggested some interesting solutions but this can also be done mathematically using a simple observation. Notice that:

其他人提出了一些有趣的解决方案,但这也可以使用简单的观察在数学上完成。请注意:

1 - 1*1

1 - 1*1

22 - 2*11

22 - 2*11

333 - 3*111

333 - 3*111

4444 - 4*1111

4444 - 4*1111

and so on ....

等等 ....

We can have a general formula for producing 1,11,111,1111,... at every iteration. Observe that:

我们可以有一个通用公式来在每次迭代中产生 1,11,111,1111,...。请注意:

1 = 9/9 = (10 - 1)/9 = (10^1 - 1)/9

1 = 9/9 = (10 - 1)/9 = (10^1 - 1)/9

11 = 99/9 = (100 - 1)/9 = (10^2 - 1)/9

11 = 99/9 = (100 - 1)/9 = (10^2 - 1)/9

111 = 999/9 = (1000 - 1)/9 = (10^3 - 1)/9

111 = 999/9 = (1000 - 1)/9 = (10^3 - 1)/9

......

......

that is we have (10^i - 1)/9 for the ith iteration.

也就是说,我们有 (10^i - 1)/9 用于第 i 次迭代。

Now it is simple enough to implement. We will multiply i with the above formula in each iteration. Hence the overall formula is:

现在它很容易实现。我们将在每次迭代中将 i 与上述公式相乘。因此总的公式为:

i*(10^i - 1)/9 (for every ith iteration). Here's the python code:

i*(10^i - 1)/9(对于每一次迭代)。这是python代码:

for i in xrange(1,10):
    print i*(10**i-1)/9

Hope this helps.

希望这可以帮助。

回答by Tejumade

I realised that the problem is solved but here's how you wanted your code to look like.

我意识到问题已解决,但这是您希望代码的样子。

count=0
for i in range(10):
    for j in range(0, i):
        print (count, end='')
count +=1
print()

i think @Dannnno answer is shorter and straight to the point :)

我认为@Dannnno 的回答更短而且直截了当:)

回答by Vasanth Vijayabaskar

The simple mistake in your code is the placement of count = count + 1. It should be placed after the second for loop block. I have made a simple change in your own code to obtain the output you want.

代码中的一个简单错误是 count = count + 1 的位置。它应该放在第二个 for 循环块之后。我对您自己的代码做了一个简单的更改以获得您想要的输出。

    from __future__ import print_function
    count = 0
    for i in range(10):
        for j in range(0, i):
            print(count,end='')
        count = count +1
    print()

This will give the output you want with the code you wrote. :)

这将使用您编写的代码提供您想要的输出。:)