简体   繁体   English

遍历Python列表中的“积压”

[英]“Backlog” of a loop through a list in Python

I have a weird Python beginner's question,.. playing around in my virtual env in the interpreter (python 3.5): 我有一个奇怪的Python初学者问题,..在解释器(python 3.5)的虚拟环境中玩耍:

I have a list with mixed types: 我有一个混合类型的列表:

lissi = ["foo", "bar". "boo", "baz", 4, 7]

And then "accidentally" try to print out all elements in a for loop concatenated to a string: 然后“偶然地”尝试打印出for循环中连接到字符串的所有元素:

for x in lissi:
   print("Hallo " + x)

This, of course, is not possible bcs. 当然,这不可能。 we cannot conc. 我们不能否认。 integers to a String - so the first elements are printed and then there is a TypeError. 整数到字符串-因此先打印元素,然后出现TypeError。

Then I typed just "x" and enter to see if there is still data stored and yes it is: x is 4 . 然后,我只键入"x" and enter以查看是否仍然存储有数据,是的: x为4

type(x) is int (tried that to figure out if 7 is also still there). type(x)int (试图找出是否还存在7)。

So my question is: What happens "under the hood" in Python in the for loop: Seems as if every successfully processed element is removed, but there is a backlog stored in x which is the first element the TypeError was thrown for? 所以我的问题是:for循环中Python的“幕后”会发生什么:似乎每个成功处理的元素都被删除了,但是x中存储了一个积压,这是抛出TypeError的第一个元素? And is there a way to "clear" this data from memory in case of errors? 如果有错误,是否有办法从内存中“清除”此数据?

thx 谢谢

The for loop is part of the scope it is declared in, so the following code will change the value of x : for循环是在其中声明的作用域的一部分,因此以下代码将更改x的值:

x = 9

for x in xrange(3): # iterate 0, 1, 2
    print(x)

print(x) # x == 2, x != 9

When the element was "baz" , everything was okay and the print statement worked. 当元素为"baz" ,一切正常,并且打印语句起作用。 Python then continued execution. 然后,Python继续执行。 The next step was x = 4 . 下一步是x = 4 After that, print "Hallo" + x failed with an error. 之后, print "Hallo" + x失败并显示错误。

While running the interpreter, errors are caught and printed, then execution continues . 运行解释器时,会捕获并打印错误,然后继续执行 There is no cleanup after an error in the interpreter, so the last value of x is still there when you check the value. 解释器中的错误发生后不会进行清理,因此x的最后一个值在您检查该值时仍然存在。 This is why you see 4. 这就是为什么您看到4。

It's not a backlog and nothing like "every successfully processed element is removed". 这不是积压,也不像“删除每个成功处理的元素”。
Basically on every iteration for loop assigns to variable x the value of next element of list lissi (doesn't have to be a list, can be any iterable). 基本上在每个迭代上for循环分配给变量x列表的下一个元素的值lissi (不必是一个列表,可以是任何可迭代)。
Whenever the loop breaks, due to exception or break statement, variable x is left intact, so it contains the last value assigned by the for loop. 每当循环中断时,由于异常或break语句,变量x保持不变,因此它包含由for循环分配的最后一个值。
This is ok, since loops don't have any isolated scope in Python and actually makes it convenient to search iterable for interesting element, breaking the loop when it's found and not being forced to store it in another variable before leaving the loop. 没关系,因为循环在Python中没有任何孤立的作用域,实际上可以方便地在可迭代元素中搜索有趣的元素,在找到循环时中断循环,并且在离开循环之前不被迫将其存储在另一个变量中。

I believe the problem is not with the for loop, but with the print() statement. 我相信问题不在于for循环,而在于print()语句。

You cannot + a string with an integer , example: 您不能+带有integerstring ,例如:

This will not work: 这将不起作用:

print("Hallo " + 4)

nor will this: 也不会:

print(4 + " Hallo")

if it is all integers it will work: 如果都是整数,它将起作用:

print(4 + 1)

the error which is shown from print("Hallo " + 4) is " builtins.TypeError: Can't convert 'int' object to str implicitly " print("Hallo " + 4)显示的错误是“ builtins.TypeError: Can't convert 'int' object to str implicitly

The solution is to do the conversion from integer to string explicitely: 解决方案是显式地进行从整数到字符串的转换:

for x in lissi:
   print("Hallo " + str(x))

Now, for your question, I do not believe there is something as a "back log". 现在,对于您的问题,我不认为有什么东西可以作为“备用日志”。 The enumeration of for x in lissi is still valid, and x is still valid, just that while processing the first 4 enumerations (where x is a string ) the print() statement will work, then it throws an error on the print() statement, but x is still a valid integer . for x in lissifor x in lissi的枚举仍然有效,并且x仍然有效,只是在处理前4个枚举(其中xstring )时, print()语句将起作用,然后在print()上引发错误语句,但是x仍然是有效的integer

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

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