英文:
What is the meaning of reverse=[i]+reverse
问题
reverse = [i] + reverse 这行代码的意思是将当前迭代的元素 i 放在已经反转部分的列表 reverse 的前面。它实际上是在逐步地构建一个反转后的列表。这是一种逐步反转列表的方法,不使用任何内置函数或方法。
英文:
I'm writing code in python to reverse a string and stumble upon this code.
lst = [21,2,3,1,96,55,66,12,19]
reverse = []
for i in lst:
print([i])
reverse = [i] + reverse
Wants to know what is the meaning of reverse=[i]+reverse, what does it do ?
I'm trying to reverse a list without using any function or built in methods.
I was thinking of something simple which doesn't involve any functions.
答案1
得分: 2
[i]
只是一个包含一个元素i
的列表字面值。- 在此之上调用
+ reverse
,以生成一个新列表,该列表以i
开始,并以来自reverse
列表的所有元素结尾。 - 最后,
reverse
被重新赋值以指向这个新列表。
实际上,这是逐个元素地构建反转列表。
英文:
[i]
is just a list literal containing one element,i
.+ reverse
is called on that, to produce a new list that starts withi
, and
ends with all the elements from thereverse
list.- Finally,
reverse
is re-assigned to point to this new list.
In effect, this is building up the reversed list one element at a time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论