2013-09-25 119 views
0

蟒蛇新手再次在这里,python:pop()while while循环返回错误

我想重复删除列表的最后一个条目,直到它返回一个特定的字符。但试图运行脚本,我得到“IndexError:从空列表中弹出”。那么不知何故,该列表不包含在while循环中?

CODE:

theList = list("abc/123") 

popStop = "" 
popped = "" 

while popStop != "/": 
    if theList.pop() != "/": 
     popped = popped + str(theList.pop()) 
    else: 
    popStop = "/" 

预先感谢任何帮助。

回答

11

你是poping同一个项目的两倍。你有可能是打算

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

想像的强

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

for e in reversed(theList): 
    if e != '/': 
     popped += e 
    else: 
     popStop = e 
     break 

,然后你开始寻找周围的Python库,并实现它有一个极好的工具,可迭代称为itertools,所以你最终编写使用takewhile

from itertools import takewhile 
popped = ''.join(takewhile(lambda e: e != '/', reversed(theList))) 

而现在另一个版本有更多的经验,你很快就会意识到,你实际上正在分裂一条路径,而Python足以让你为它保留一个函数库(os.path.split)。

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

并在平均时间你已经介绍自己PEP-8,当你意识到官方的风格指南,首字母大写命名变量不是Python的。

然后你结束了一个漂亮的一个班轮

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

呃......前面的路很长。非常感谢你。有趣的进展和很好的解释。 :) – user2808964

0

这种基于一流的解决方案将使招并产生每一个项目,只要你想基于一个通用stop_item元素:

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

''' 
stackoverflow_19012268.py 

@author: Luis Martin Gil 
@contact: [email protected] 

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 
    # ---------------------------------------- 
    # ---------------------------------------- 
    # ---------------------------------------- 

获取代码here