Python:pop()在while循环中返回错误

Python:pop()在while循环中返回错误,python,while-loop,runtime-error,Python,While Loop,Runtime Error,这里又是python新手 我试图反复删除列表的最后一个条目,直到它返回某个字符。但尝试运行脚本时,我从空列表中得到indexer:pop。所以不知何故,列表不包括在while循环中 代码: 提前感谢您的帮助。您将同一项目弹出两次。你可能是有意的 while popStop != "/": item = theList.pop() if item != "/": popped = popped + str(item) else: popStop = "/"

这里又是python新手

我试图反复删除列表的最后一个条目,直到它返回某个字符。但尝试运行脚本时,我从空列表中得到indexer:pop。所以不知何故,列表不包括在while循环中

代码:

提前感谢您的帮助。

您将同一项目弹出两次。你可能是有意的

while popStop != "/":
   item = theList.pop()
   if item != "/":
      popped = popped + str(item)
   else:
     popStop = "/"
深入思考

有了一点经验,您很快就会意识到,上面的代码不是很像Python。可以使用for循环编写更好的循环结构

然后您开始查看Python库,发现它有一个漂亮的iterable工具,名为

现在有了更多的经验,您很快就会意识到,您实际上是在分割一条路径,Python很好地为您保留了一个库函数

os.path.split(theList)[-1][::-1]
同时,当您意识到CamelCase中的命名变量不是Pythonic时,您已经向官方样式指南介绍了自己

然后你会得到一个漂亮的一行

os.path.split(the_list)[-1][::-1]

此基于类的解决方案将根据通用的stop_item元素生成您想要的每一项:

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
stackoverflow_19012268.py

@author:  Luis Martin Gil
@contact: martingil.luis@gmail.com

https://github.com/luismartingil
www.luismartingil.com
'''

class MyStopReversedList(list):
    """ Implements a list based on a reversed way to iterate over it.
    """
    def __init__(self, stop_item, list):
        self.stop_item = stop_item
        super(MyStopReversedList, self).__init__(list)

    def __iter__(self):
        """ Iterates the list until it reaches stop_item """
        while True:
            try:
                item = list.pop(self)
                if item is self.stop_item: break
                else: yield item
            except:
                break

if __name__ == "__main__":
    # Lets work on some examples
    examples = [
        {
            # Example1. Integers list
            'stop_item' : 3,
            'my_list' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 10, 11, 12, 13]
            },
        {
            # Example2. String
            'stop_item' : '/',
            'my_list' : "abc/123"
            }
        ]

    for example in examples:
        the_list = MyStopReversedList(example['stop_item'],
                                      example['my_list'])
        # Lets try to iterate over the list n times
        n = 4
        print 'Example', example
        for iteration in range(n):
            for item in the_list:
                print '(iteration:%i) %s' % (iteration, item)
            print '-' * 40
        print '\n'

    # Outputs
    # Example {'my_list': [1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 10, 11, 12, 13], 'stop_item': 3}
    # (iteration:0) 13
    # (iteration:0) 12
    # (iteration:0) 11
    # (iteration:0) 10
    # ----------------------------------------
    # (iteration:1) 9
    # (iteration:1) 8
    # (iteration:1) 7
    # (iteration:1) 6
    # (iteration:1) 5
    # (iteration:1) 4
    # ----------------------------------------
    # (iteration:2) 2
    # (iteration:2) 1
    # ----------------------------------------
    # ----------------------------------------


    # Example {'my_list': 'abc/123', 'stop_item': '/'}
    # (iteration:0) 3
    # (iteration:0) 2
    # (iteration:0) 1
    # ----------------------------------------
    # (iteration:1) c
    # (iteration:1) b
    # (iteration:1) a
    # ----------------------------------------
    # ----------------------------------------
    # ----------------------------------------

获取代码

好吧。。。前面的路很长。谢谢你。有趣的进展和很好的解释
os.path.split(theList)[-1][::-1]
os.path.split(the_list)[-1][::-1]
#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
stackoverflow_19012268.py

@author:  Luis Martin Gil
@contact: martingil.luis@gmail.com

https://github.com/luismartingil
www.luismartingil.com
'''

class MyStopReversedList(list):
    """ Implements a list based on a reversed way to iterate over it.
    """
    def __init__(self, stop_item, list):
        self.stop_item = stop_item
        super(MyStopReversedList, self).__init__(list)

    def __iter__(self):
        """ Iterates the list until it reaches stop_item """
        while True:
            try:
                item = list.pop(self)
                if item is self.stop_item: break
                else: yield item
            except:
                break

if __name__ == "__main__":
    # Lets work on some examples
    examples = [
        {
            # Example1. Integers list
            'stop_item' : 3,
            'my_list' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 10, 11, 12, 13]
            },
        {
            # Example2. String
            'stop_item' : '/',
            'my_list' : "abc/123"
            }
        ]

    for example in examples:
        the_list = MyStopReversedList(example['stop_item'],
                                      example['my_list'])
        # Lets try to iterate over the list n times
        n = 4
        print 'Example', example
        for iteration in range(n):
            for item in the_list:
                print '(iteration:%i) %s' % (iteration, item)
            print '-' * 40
        print '\n'

    # Outputs
    # Example {'my_list': [1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 10, 11, 12, 13], 'stop_item': 3}
    # (iteration:0) 13
    # (iteration:0) 12
    # (iteration:0) 11
    # (iteration:0) 10
    # ----------------------------------------
    # (iteration:1) 9
    # (iteration:1) 8
    # (iteration:1) 7
    # (iteration:1) 6
    # (iteration:1) 5
    # (iteration:1) 4
    # ----------------------------------------
    # (iteration:2) 2
    # (iteration:2) 1
    # ----------------------------------------
    # ----------------------------------------


    # Example {'my_list': 'abc/123', 'stop_item': '/'}
    # (iteration:0) 3
    # (iteration:0) 2
    # (iteration:0) 1
    # ----------------------------------------
    # (iteration:1) c
    # (iteration:1) b
    # (iteration:1) a
    # ----------------------------------------
    # ----------------------------------------
    # ----------------------------------------