简体   繁体   English

这个 python 代码有什么问题? (初学者的问题)

[英]What's wrong with this python code? (beginner's question)

What is wrong with this code?这段代码有什么问题? I must be missing something trivial.我一定错过了一些微不足道的东西。 Every time I try to run it it does nothing for a while and then outputs 'Killed'.每次我尝试运行它时,它都会有一段时间什么都不做,然后输出“Killed”。 I wanted it to take every element in list, add "x" to it and then append this new element to the list, so that output would look like this:我希望它获取列表中的每个元素,将“x”添加到它,然后将 append 这个新元素添加到列表中,这样 output 将如下所示:

['a', 'b', 'c', 'd', 'ax', 'bx', 'cx', 'dx']

My code so far:到目前为止我的代码:

list = ['a', 'b', 'c', 'd']

for element in list:
    element = element + "x"
    list.append(element)

print(list)

You're appending to your list as you iterate over it, so every time you take a "step forward", you add another "step" to take later, so you're ending up with ['a', 'b', 'c', 'd', 'ax', 'bx', 'cx', 'dx', 'axx', 'bxx'...] .当您遍历它时,您将附加到您的列表中,因此每次您向前迈出“一步”时,您都会添加另一个“步骤”以便稍后采取,因此您最终会得到['a', 'b', 'c', 'd', 'ax', 'bx', 'cx', 'dx', 'axx', 'bxx'...] For a whole host of reasons similar to this, a general rule is you should avoid modifying a list as you iterate over it.出于与此类似的许多原因,一般规则是您应该避免在迭代列表时修改列表。

Try this instead试试这个

list_1 = ['a', 'b', 'c', 'd']

list_2 = [elem + 'x' for elem in list_1]

result = list_1 + list_2

print(result)
list = ['a', 'b', 'c', 'd']
list2 = []
for element in list:
    list2.append(element + "x")
list.extend(list2)

print(list)

Since you were appending inside the loop you used to get the memory error.由于您在循环中附加,因此您曾经得到 memory 错误。 The above code might help you.上面的代码可能会对你有所帮助。

After appending to the list2, i just added it to original list1, and got the desired result.附加到 list2 后,我只是将其添加到原始 list1 中,并得到了所需的结果。

list1 = ['a', 'b', 'c', 'd']
list2 = []

for x in list1:
    list2.append(x+'x')

print(list1+list2)  

What's wrong with this code?这段代码有什么问题? The answer is you have created an infinite loop since you continously add an element to a list as you iterate over it.答案是您创建了一个无限循环,因为您在迭代时不断地将元素添加到列表中。

A cleaner way to do this would be to use extend一种更简洁的方法是使用extend

So you could do something like this:所以你可以做这样的事情:

l = ['a','b','c','d'] # DO NOT USE list as a variable name, it is a keyword in python

l.extend([element + 'x' for element in l])

print(l)

Also, the reason your code doesn't work is because you are essentially creating an infinite loop, because your loop keeps on adding elements to the list you are iterating over.此外,您的代码不起作用的原因是因为您实际上是在创建一个无限循环,因为您的循环不断将元素添加到您正在迭代的列表中。

Using extend in the way I have mentioned above, would create a temporary list with the new items, and then add every item from the temporary list into the actual list.以我上面提到的方式使用扩展,将创建一个包含新项目的临时列表,然后将临时列表中的每个项目添加到实际列表中。

Don't use list , it is a keyword (as an example see the copy function I used in line 2)不要使用list ,它是一个关键字(例如,请参阅我在第 2 行中使用的副本 function)

You are appending to a list while iterating it, so this will result in an endless loop您在迭代时附加到列表,因此这将导致无限循环

base_list = ['a', 'b', 'c', 'd']
new_list = list.copy(base_list)
for element in base_list:
    newelement = element + 'x'
    new_list.append(newelement)
print(new_list)

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

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