标签:
列表也是序列的一种。列表能保存任意数目的Python对象,列表是可变类型。
列表可以使用[]来创建,或者使用工厂方法list()来创建。
>>> t = list()
>>> type(t)
<type ‘list‘>
>>> l = []
>>> type(l)
<type ‘list‘>
>>> t == l
True
>>> t = list(‘furzoom‘)
>>> t
[‘f‘, ‘u‘, ‘r‘, ‘z‘, ‘o‘, ‘o‘, ‘m‘]
>>> t[1]
‘u‘
>>> t[2] = ‘n‘
>>> t
[‘f‘, ‘u‘, ‘n‘, ‘z‘, ‘o‘, ‘o‘, ‘m‘]
>>> t.append(‘.‘)
>>> t
[‘f‘, ‘u‘, ‘n‘, ‘z‘, ‘o‘, ‘o‘, ‘m‘, ‘.‘]
>>> del t[3]
>>> t
[‘f‘, ‘u‘, ‘n‘, ‘o‘, ‘o‘, ‘m‘, ‘.‘]
>>> del t
>>> t
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘t‘ is not defined
支持比较运算、切片[]或者[:]、in, not in、连接操作符+、重复操作。
如果可以,尽量使用list.extend()方式代替连接操作符。
列表还支持非常重要的列表解析操作。
>>> [i for i in xrange(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
比较原则:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
stack = []
def pushit():
stack.append(raw_input(‘Enter New string: ‘).strip())
def popit():
if len(stack) == 0:
print ‘Cannot pop from an empty stack!‘
else:
print ‘Removed [‘, `stack.pop()`, ‘]‘
def viewstack():
print stack
CMDs = {‘u‘: pushit, ‘o‘: popit, ‘v‘: viewstack}
def showmenu():
pr = """
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: """
while True:
while True:
try:
choice = raw_input(pr).strip()[0].lower()
except (EOFError, KeyboardInterrupt, IndexError):
choice = ‘q‘
print ‘\nYou picked: [%s]‘ % choice
if choice not in ‘uovq‘:
print ‘Invalid option, try again‘
else:
break
if choice == ‘q‘:
break
CMDs[choice]()
if __name__ == ‘__main__‘:
showmenu()
运行示例如下:
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: u
You picked: [u]
Enter New string: Python
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: u
You picked: [u]
Enter New string: is
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: u
You picked: [u]
Enter New string: cool!
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: v
You picked: [v]
[‘Python‘, ‘is‘, ‘cool!‘]
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: o
You picked: [o]
Removed [ ‘cool!‘ ]
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: o
You picked: [o]
Removed [ ‘is‘ ]
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: o
You picked: [o]
Removed [ ‘Python‘ ]
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: o
You picked: [o]
Cannot pop from an empty stack!
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: ^D
You picked: [q]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
queue = []
def enQ():
queue.append(raw_input(‘Enter New string: ‘).strip())
def deQ():
if len(queue) == 0:
print ‘Cannot pop from an empty queue!‘
else:
print ‘Removed [‘, `queue.pop(0)`, ‘]‘
def viewQ():
print queue
CMDs = {‘e‘: enQ, ‘d‘: deQ, ‘v‘: viewQ}
def showmenu():
pr = """
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: """
while True:
while True:
try:
choice = raw_input(pr).strip()[0].lower()
except (EOFError, KeyboardInterrupt, IndexError):
choice = ‘q‘
print ‘\nYou picked: [%s]‘ % choice
if choice not in ‘edvq‘:
print ‘Invalid option, try again‘
else:
break
if choice == ‘q‘:
break
CMDs[choice]()
if __name__ == ‘__main__‘:
showmenu()
运行示例如下:
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: e
You picked: [e]
Enter New string: Bring out
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: e
You picked: [e]
Enter New string: your dead!
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: v
You picked: [v]
[‘Bring out‘, ‘your dead!‘]
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: d
You picked: [d]
Removed [ ‘Bring out‘ ]
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: d
You picked: [d]
Removed [ ‘your dead!‘ ]
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: d
You picked: [d]
Cannot pop from an empty queue!
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: ^D
You picked: [q]
标签:
原文地址:http://blog.csdn.net/furzoom/article/details/51922060