简体   繁体   English

获取不同的 output 以在 python 3 中对我的打印语句进行不同定位

[英]Getting different output for different positioning of my print statement in python 3

This is the normal code i was writing and this code has the desired output i wanted这是我正在编写的普通代码,此代码具有我想要的 output

a = [1,1,2,3,5,8,4,13,21,34,55,89]
less5num = []
for i in a:
    if i < 5:
        less5num.append(i)
        less5num.sort()
print(less5num)

Output
[1, 1, 2, 3, 4]

now, if i keep print inside the if loop i get a number pyramid现在,如果我在 if 循环中保持打印,我会得到一个数字金字塔

a = [1,1,2,3,5,8,4,13,21,34,55,89]
less5num = []
for i in a:
    if i<5:
        less5num.append(i)
        less5num.sort()
        print(less5num)

Output
[1]
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3, 4]

if i keep print out of the if loop i get a recursive number pyramid如果我不打印 if 循环,我会得到一个递归数字金字塔

a = [1,1,2,3,5,8,4,13,21,34,55,89]
less5num = []
for i in a:
    if i<5:
        less5num.append(i)
        less5num.sort()
    print(less5num)

Output
[1]
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3]
[1, 1, 2, 3]
[1, 1, 2, 3, 4]
[1, 1, 2, 3, 4]
[1, 1, 2, 3, 4]
[1, 1, 2, 3, 4]
[1, 1, 2, 3, 4]
[1, 1, 2, 3, 4]

why is this hapenning?为什么会这样?

a = [1,1,2,3,5,8,4,13,21,34,55,89]
less5num = []
for i in a:
    if i<5:
        less5num.append(i)
        less5num.sort()
    # this print in loop but outside if statement
    print(less5num)

Your print is in loop but outside if statement.您的打印在循环中,但在 if 语句之外。 That's why all iterations are print in console这就是为什么所有迭代都在控制台中打印

In your first block of code, the print is not inside the for loop, it is executed after all the elements are iterated, only the numbers less than 5 are added to list and sorted.在您的第一个代码块中,打印不在for循环内,它在所有元素都被迭代后执行,只有小于 5 的数字被添加到列表中并排序。

In your second block of code, the print is inside the loop , and also only executes if number is less than 5 , so only less number of times print is executed.在您的第二个代码块中, print 位于loop内,并且仅number小于 5时执行,因此 print 执行的次数更少。

In your 3rd block of code, your print is in the for loop , but not within the if block, so when every element is encountered, a printing happens, so as many number of times as there are numbers in your list.在您的第三个代码块中,您的 print 位于for loop中,但不在if块中,因此当遇到每个元素时,都会发生打印,因此与列表中的数字一样多。

If the print is outside the loop, it prints the list once with all numbers less than 5.如果print在循环之外,它会打印一次所有数字小于 5 的列表。

If it's inside the if you print the list once for every time you find a number less than 5. The list is mid creation so it grows larger.如果它在里面, if每次发现小于 5 的数字时打印一次列表。列表是中间创建的,所以它会变大。

If it's inside the loop, you print it once for every number in the loop.如果它在循环内,则为循环中的每个数字打印一次。

You can visualize it using pythontutor .您可以使用pythontutor将其可视化。

First if首先如果

There is 5 elements in list less than 5 so you print it 5 times.列表中有 5 个元素小于 5,因此您打印 5 次。

[1,1,2,3,4]

Second if第二个如果

Loop ends after all value in list.循环在列表中的所有值之后结束。 There is 12 elements in list so you print it 12 times.列表中有 12 个元素,因此您打印 12 次。

[1,1,2,3,5,8,4,13,21,34,55,89]

Thankyou guys it helped a lot!!谢谢大家,帮了大忙!!

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

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