demo.
- >>> a=[1,2]
- >>> a.append(3)#追加到链尾
- >>> a
- [1, 2, 3]
- >>> b=[4,5]
- >>> a.extend(b)#追加一个列表
- >>> a
- [1, 2, 3, 4, 5]
- >>> a.insert(0,9)#在位置0插入9
- >>> a
- [9, 1, 2, 3, 4, 5]
- >>> a.remove(9)#删除值为”9″的元素(如果存在多个9,则只删除第一个),如果不存在会抛出异常
- >>> a
- [1, 2, 3, 4, 5]
- >>> a.reverse()#反转序列
- >>> a
- [5, 4, 3, 2, 1]
- >>> a.pop(1)#返回并删除位置为i的元素,i默认为最后一个元素
- 4
- >>> a
- [5, 3, 2, 1]
- >>> a.index(2)#返回第一个值为’2’的元素,不存在则抛出异常
- 2
- >>> a
- [5, 3, 2, 1]
- >>> a.count(5)#统计一个字符出现的次数
- 1
- >>> a.sort()#排序数字和字母各排各的,按升序排列
- >>> a
- [1, 2, 3, 5]
- >>> a[0:3]#包括a[0],不包括a[3]
- [1, 2, 3]
- >>> a[:-1]#包括开始,不包括最后一个
- [1, 2, 3]
- >>> a[-3:]#抽取最后3个
- [2, 3, 5]
- >>> a[::-1]#倒序排列
- [5, 3, 2, 1]
- >>> a[::]#正序排列
- [1, 2, 3, 5]
Python代码
- >>> fruit=[‘ apple ‘,’ orange ‘]
- >>> fruit
- [‘ apple ‘, ‘ orange ‘]
- >>> [fruit.strip() for fruit in fruit]
- [‘apple’, ‘orange’]
- >>> a
- [1, 2, 3, 5]
- >>> [3*x for x in a if x>3]#3*x代表3乘以x
- [15]
- >>> [(x,x**2) for x in a]#x**2代表x的平方
- [(1, 1), (2, 4), (3, 9), (5, 25)]
- >>> a
- [1, 2, 3, 5]
- >>> b
- [4, 5]
- >>> [x*y for x in a for y in b]
- [4, 5, 8, 10, 12, 15, 20, 25]
- >>> [a[i]+b[i] for i in range(len(b))]#len(b)为2,所以为a[0]+b[0],a[1]+b[1]
- [5, 7]
- >>> [str(round(200/110.0,i)) for i in range(1,10)]#str()是转换类型为可以打印的字符,round(x,n)表示对x保留n位小数(四舍五入)
- [‘1.8’, ‘1.82’, ‘1.818’, ‘1.8182’, ‘1.81818’, ‘1.818182’, ‘1.8181818’, ‘1.81818182’, ‘1.818181818’]
Python代码
- >>> test=’abc’,123,’school’#test=(‘abc’,123,’school’)的简写
- >>> x,y,z=test#拆分操作可以应用于所有sequence
- >>> x
- ‘abc’
- >>> y
- 123
- >>> z
- ‘school’
- >>> union = test,(1,2,3)
- >>> union
- ((‘abc’, 123, ‘school’), (1, 2, 3))
- >>> empty=()#空元组
- >>> singleton=’hi’,#单个元素的元组,注意逗号
- >>> singleton
- (‘hi’,)
- >>> url=set(‘http://sucre.javaeye.com’)
- >>> url
- set([‘a’, ‘o’, ‘c’, ‘e’, ‘h’, ‘j’, ‘m’, ‘/’, ‘.’, ‘p’, ‘s’, ‘r’, ‘u’, ‘t’, ‘v’, ‘y’, ‘:’])
- >>> url1=set(‘http://hi.baidu.com/beijingalana’)
- >>> url1
- set([‘a’, ‘o’, ‘c’, ‘b’, ‘e’, ‘d’, ‘g’, ‘i’, ‘h’, ‘j’, ‘m’, ‘l’, ‘/’, ‘.’, ‘p’, ‘n’, ‘u’, ‘t’, ‘:’])
- >>> url-url1#差
- set([‘y’, ‘s’, ‘r’, ‘v’])
- >>> url|url1#并
- set([‘/’, ‘n’, ‘a’, ‘c’, ‘b’, ‘e’, ‘d’, ‘g’, ‘i’, ‘h’, ‘j’, ‘m’, ‘l’, ‘o’, ‘.’, ‘p’, ‘s’, ‘r’, ‘u’, ‘t’, ‘v’, ‘y’, ‘:’])
- >>> url&url1#交
- set([‘a’, ‘c’, ‘e’, ‘h’, ‘j’, ‘m’, ‘o’, ‘.’, ‘p’, ‘u’, ‘/’, ‘:’, ‘t’])
- >>> a^b#a,b都为元组,^对元组无效
- Traceback (most recent call last):
- File “<interactive input>”, line 1, in <module>
- TypeError: unsupported operand type(s) for ^: ‘list’ and ‘list’
- >>> url
- set([‘a’, ‘o’, ‘c’, ‘e’, ‘h’, ‘j’, ‘m’, ‘/’, ‘.’, ‘p’, ‘s’, ‘r’, ‘u’, ‘t’, ‘v’, ‘y’, ‘:’])
- >>> url.remove(‘:’)
- >>> url
- set([‘a’, ‘o’, ‘c’, ‘e’, ‘h’, ‘j’, ‘m’, ‘/’, ‘.’, ‘p’, ‘s’, ‘r’, ‘u’, ‘t’, ‘v’, ‘y’])
- >>> url1
- set([‘a’, ‘o’, ‘c’, ‘b’, ‘e’, ‘d’, ‘g’, ‘i’, ‘h’, ‘j’, ‘m’, ‘l’, ‘/’, ‘.’, ‘p’, ‘n’, ‘u’, ‘t’, ‘:’])
- >>> url1.remove(‘:’)
- >>> url
- set([‘a’, ‘o’, ‘c’, ‘e’, ‘h’, ‘j’, ‘m’, ‘/’, ‘.’, ‘p’, ‘s’, ‘r’, ‘u’, ‘t’, ‘v’, ‘y’])
- >>> url1
- set([‘a’, ‘o’, ‘c’, ‘b’, ‘e’, ‘d’, ‘g’, ‘i’, ‘h’, ‘j’, ‘m’, ‘l’, ‘/’, ‘.’, ‘p’, ‘n’, ‘u’, ‘t’])
- >>> url^url1#(并-交)
- set([‘n’, ‘b’, ‘d’, ‘g’, ‘i’, ‘l’, ‘s’, ‘r’, ‘v’, ‘y’])
- >>> url.add(‘:’)
- >>> url
- set([‘a’, ‘o’, ‘c’, ‘e’, ‘h’, ‘j’, ‘m’, ‘/’, ‘.’, ‘p’, ‘s’, ‘r’, ‘u’, ‘t’, ‘v’, ‘y’, ‘:’])
- >>> url1.add(‘:’)
- >>> url^url1
- set([‘n’, ‘b’, ‘d’, ‘g’, ‘i’, ‘l’, ‘s’, ‘r’, ‘v’, ‘y’])
- >>> url.pop(-1)
- Traceback (most recent call last):
- File “<interactive input>”, line 1, in <module>
- TypeError: pop() takes no arguments (1 given)
- >>> url.pop()#对于列表用pop默认去掉的竟然是第一个
- ‘a’
- >>> url
- set([‘o’, ‘c’, ‘e’, ‘h’, ‘j’, ‘m’, ‘/’, ‘.’, ‘p’, ‘s’, ‘r’, ‘u’, ‘t’, ‘v’, ‘y’, ‘:’])
- >>> a
- [1, 2, 3, 5]
- >>> a.pop()
- 5