Python的list真hi。。。
list实现一个栈:在数据结构中提到,c可以使用双队列mock,
在Java中使用单个List+API完全可以做到,
用Python。。则完全不用做事了:list居然实现了一个典型栈才有的方法pop()...真是动态到了极致。
[*]stack = []
[*]print(type(stack))
[*]
[*]def push():
[*] stack.append(raw_input('Enter New String Node').srip())
[*]
[*]def pop():
[*] if len(stack)==0:
[*] print 'Empty stack Error'
[*] else:
[*] print 'remove ',`stack.pop()`
列表的解析也是极大减少了指头的机械性损伤:
[*]#列表解析
[*]squared =
[*]sqdEvens =
下面是一些切片操作:切片在string,list,tuple中都是可以使用的
[*]##列表切片操作
[*]test=['never', 1, 2, 'yes', 1, 'no', 'maybe']
[*]
[*]test #包括test,不包括test
[*]['never', 1, 2]
[*]
[*]test #包括test,不包括test,而且步长为2
[*]['never', 2, 1]
[*]
[*]test[:-1] #包括开始,不包括最后一个
[*]['never', 1, 2, 'yes', 1, 'no']
[*]
[*]test[-3:] #抽取最后3个
[*]
[*]
[*]test[::-1] #倒序排列
[*]['maybe', 'no', 1, 'yes', 2, 1, 'never']
页:
[1]