List,Tuple
1. 转换
我们可以使用 list() / tuple() 函数在列表和元组之间进行转换。
这两个函数接受字符串参数时候比较有意思。
2. 运算符操作
列表支持运算符操作。
>>>>>> [1, 2] * 2
[1, 2, 1, 2]
>>>>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
[1, 2, 1, 2]
>>>>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
3. in / not in
可以使用 in / not in 来判断是否包含某个元素。
4. range()
我们还可以使用 range() 函数获得一个整数列表,甚至进行运算和添加过滤条件。
>>>>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>>>> range(2, 10, 2)
[2, 4, 6, 8]
>>>>>> range(2, 7)
[2, 3, 4, 5, 6]
>>>>>> [x*2 for x in range(10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>>>> [x for x in range(10) if x%2>0]
[1, 3, 5, 7, 9]
>>>>>> [x + 1 for x in range(10) if x%2==0]
[1, 3, 5, 7, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>>>> range(2, 10, 2)
[2, 4, 6, 8]
>>>>>> range(2, 7)
[2, 3, 4, 5, 6]
>>>>>> [x*2 for x in range(10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>>>> [x for x in range(10) if x%2>0]
[1, 3, 5, 7, 9]
>>>>>> [x + 1 for x in range(10) if x%2==0]
[1, 3, 5, 7, 9]
5. Slices
和字符串一样,我们可以通过序号或切片进行访问。
>>>>>> b = (1,2,3)
>>>>>> b[-1]
3
>>>>>> b[1:-1]
(2,)(2,)
>>>>>> b=(1,2,3)
>>>>>> b[1]
2
>>>>>> b[1:]
(2, 3)(2, 3)
>>>>>> b[-1]
3
>>>>>> b=[1,2,3]
>>>>>> b[1] = 100
>>>>>> b
[1, 100, 3]
>>>>>> b[-1]
3
>>>>>> b[1:-1]
(2,)(2,)
>>>>>> b=(1,2,3)
>>>>>> b[1]
2
>>>>>> b[1:]
(2, 3)(2, 3)
>>>>>> b[-1]
3
>>>>>> b=[1,2,3]
>>>>>> b[1] = 100
>>>>>> b
[1, 100, 3]
6. 相关方法
基本方法:
>>>>>> a = ['a','b','c']
>>>>>> a.index('b')
1
>>>>>> a += ['d']
>>>>>> a
['a', 'b', 'c', 'd']
>>>>>> a += ['b']
>>>>>> a
['a', 'b', 'c', 'd', 'b']
>>>>>> a.count('b')
2
>>>>>> a.insert(1, 's')
>>>>>> a
['a', 's', 'b', 'c', 'd', 'b']
>>>>>> a.remove('s')
>>>>>> a
['a', 'b', 'c', 'd', 'b']
>>>>>> a.pop(2)
'c'
>>>>>> a
['a', 'b', 'd', 'b']
>>>>>> a.reverse()
>>>>>> a
['b', 'd', 'b', 'a']
>>>>>> a.sort()
>>>>>> a
['a', 'b', 'b', 'd']
>>>>>> a.extend(['e','f'])
>>>>>> a
['a', 'b', 'b', 'd', 'e', 'f']
>>>>>> a.append('m', 'n')
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
a.append('m', 'n')
TypeError: append() takes exactly one argument (2 given)
>>>>>> a.append(['m','n'])
>>>>>> a
['a', 'b', 'b', 'd', 'e', 'f', ['m', 'n']]
>>>>>> a.index('b')
1
>>>>>> a += ['d']
>>>>>> a
['a', 'b', 'c', 'd']
>>>>>> a += ['b']
>>>>>> a
['a', 'b', 'c', 'd', 'b']
>>>>>> a.count('b')
2
>>>>>> a.insert(1, 's')
>>>>>> a
['a', 's', 'b', 'c', 'd', 'b']
>>>>>> a.remove('s')
>>>>>> a
['a', 'b', 'c', 'd', 'b']
>>>>>> a.pop(2)
'c'
>>>>>> a
['a', 'b', 'd', 'b']
>>>>>> a.reverse()
>>>>>> a
['b', 'd', 'b', 'a']
>>>>>> a.sort()
>>>>>> a
['a', 'b', 'b', 'd']
>>>>>> a.extend(['e','f'])
>>>>>> a
['a', 'b', 'b', 'd', 'e', 'f']
>>>>>> a.append('m', 'n')
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
a.append('m', 'n')
TypeError: append() takes exactly one argument (2 given)
>>>>>> a.append(['m','n'])
>>>>>> a
['a', 'b', 'b', 'd', 'e', 'f', ['m', 'n']]
还可以使用 filter() 进行过滤。
>>>>>> a = range(10)
>>>>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>>>> def divfilter(i):
return i%2 == 0
>>>>>> filter(divfilter, a)
[0, 2, 4, 6, 8]
>>>>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>>>> def divfilter(i):
return i%2 == 0
>>>>>> filter(divfilter, a)
[0, 2, 4, 6, 8]
上面的代码还可以简写为:
>>>>>> filter(lambda i: i%2==0, range(10))
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
当 function 参数(第一个参数)为 None 时,可以用来过滤掉空值。
>>>>>> b = ['a', '', [], [1,2]]
>>>>>> filter(None,b)
['a', [1, 2]]
>>>>>> filter(None,b)
['a', [1, 2]]
map() 类似 .NET 中的 Array.Foreach()
>>>>>> map(lambda i:i*2, range(10))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
另外,我们还可以使用 reduce() 对元素进行统计。
>>>>>> import operator
>>>>>> reduce(operator.add, range(10))
45
>>>>>> reduce(operator.sub, [100, 5, 7])
88
>>>>>> reduce(operator.add, range(10))
45
>>>>>> reduce(operator.sub, [100, 5, 7])
88
zip() 方法可以对两个或多个列表/元组进行交叉合并。
>>>>>> zip(range(2,10), ('a', 'b', 'c', 'd', 'e'))
[(2, 'a'), (3, 'b'), (4, 'c'), (5, 'd'), (6, 'e')]