使用 list.pop() 反转列表的问题

发布于 2024-10-03 22:47:21 字数 406 浏览 6 评论 0原文

我正在编写一个小代码片段来使用列表追加和弹出来反转字符串。

我编写的脚本如下:

someStr = raw_input("Enter some string here:")
strList = []
for c in someStr:
    strList.append(c)

print strList

reverseCharList = []
for someChar in strList:
    reverseCharList.append(strList.pop())

print reverseCharList

当我输入字符串abcd时,返回的输出是[d,c]。

我知道我正在改变我正在迭代的列表,但有人可以解释为什么字符“a”和“b”没有显示在此处吗?

谢谢

I was working on writing a small code snippet to reverse a string using list appends and pop.

The script that I wrote is as follows:

someStr = raw_input("Enter some string here:")
strList = []
for c in someStr:
    strList.append(c)

print strList

reverseCharList = []
for someChar in strList:
    reverseCharList.append(strList.pop())

print reverseCharList

When I enter a string abcd, the output that's returned is [d,c].

I know I am mutating the list I am iterating over but can somebody explain why the chars 'a' and 'b' is not displayed here?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

超可爱的懒熊 2024-10-10 22:47:21

简单地反转字符串怎么样?

>>> x = 'abcd'
>>> x[::-1]
'dcba'
>>> 

在你的代码上:

永远不要改变您正在迭代的列表。它可能会导致细微的错误。

>>> strList = [1, 2, 3, 4, 5]
>>> reverseCharList = []
>>> for someChar in strList:
...     print strList
...     reverseCharList.append(strList.pop())
...     print strList
... 
[1, 2, 3, 4, 5]   <-- Iteration 1
[1, 2, 3, 4]
[1, 2, 3, 4]      <-- Iteration 2
[1, 2, 3]
[1, 2, 3]         <-- Iteration 3
[1, 2]

请参阅以下内容。由于您正在使用迭代器(for .. in ..)。
您可以直接查看迭代器详细信息以及改变列表如何与迭代器混淆。

>>> strList = [1, 2, 3, 4, 5]
>>> k = strList.__iter__()
>>> k.next()
1
>>> k.__length_hint__()   <--- Still 4 to go
4
>>> strList.pop()         <---- You pop an element
5
>>> k.__length_hint__()   <----- Now only 3 to go
3
>>> 
>>> k.next()
2
>>> k.__length_hint__()
2

How about a simple reversal of string.

>>> x = 'abcd'
>>> x[::-1]
'dcba'
>>> 

On your code:

Never mutate the list on which you are iterating with. It can cause subtle errors.

>>> strList = [1, 2, 3, 4, 5]
>>> reverseCharList = []
>>> for someChar in strList:
...     print strList
...     reverseCharList.append(strList.pop())
...     print strList
... 
[1, 2, 3, 4, 5]   <-- Iteration 1
[1, 2, 3, 4]
[1, 2, 3, 4]      <-- Iteration 2
[1, 2, 3]
[1, 2, 3]         <-- Iteration 3
[1, 2]

See the following. Since you are using iterator (for .. in ..).
You can see the iterator details directly and how mutating the list messes up with iterator.

>>> strList = [1, 2, 3, 4, 5]
>>> k = strList.__iter__()
>>> k.next()
1
>>> k.__length_hint__()   <--- Still 4 to go
4
>>> strList.pop()         <---- You pop an element
5
>>> k.__length_hint__()   <----- Now only 3 to go
3
>>> 
>>> k.next()
2
>>> k.__length_hint__()
2
花落人断肠 2024-10-10 22:47:21
for someChar in strList:
    reverseCharList.append(strList.pop())

本质上是相同的:

i = 0
while i < len(strList):
    reverseCharList.append(strList.pop())
    i += 1

第一次迭代 i 是 0,len(strList) 是 4,然后 pop+append 'd'。

第二次迭代 i 是 1,len(strList) 是 3,然后 pop+append 'c'。

第三次迭代 i 是 2,len(strList) 是 2,因此循环条件失败,您就完成了。

(这实际上是通过列表上的迭代器完成的,而不是局部变量“i”。为了清楚起见,我以这种方式展示了它。)

如果您想操纵正在迭代的序列,通常最好使用 while环形。例如:

while strList:
    reverseCharList.append(strList.pop())
for someChar in strList:
    reverseCharList.append(strList.pop())

Is essentially the same as:

i = 0
while i < len(strList):
    reverseCharList.append(strList.pop())
    i += 1

First iteration i is 0, len(strList) is 4, and you pop+append 'd'.

Second iteration i is 1, len(strList) is 3, and you pop+append 'c'.

Third iteration i is 2, len(strList) is 2, so the loop condition fails and you're done.

(This is really done with an iterator on the list, not a local variable 'i'. I've shown it this way for clarity.)

If you want to manipulate the sequence you're iterating over it's generally better to use a while loop. eg:

while strList:
    reverseCharList.append(strList.pop())
遮云壑 2024-10-10 22:47:21

当您弹出时,您会缩短列表。

reverseCharList = []
while strList:
    reverseCharList.append(strList.pop())

You shorten the list when you pop.

reverseCharList = []
while strList:
    reverseCharList.append(strList.pop())
战皆罪 2024-10-10 22:47:21

一个简单的递归版本:

def reverse(the_list):
    if not the_list:
        return []
    return [the_list.pop()] + reverse(the_list)

当然,[].reverse() 更快。

A simple recersive version:

def reverse(the_list):
    if not the_list:
        return []
    return [the_list.pop()] + reverse(the_list)

Of course, [].reverse() is faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文