转载:http://www.xwy2.com/article.asp?id=110
List,Tuple
列表(List) : 类似于 .NET ArrayList / List。
元组(Tuple) : 列表的只读版。
1. 转换
我们可以使用 list() / tuple() 函数在列表和元组之间进行转换。
Code
>>>>>> a = ['a', 'b', 'c']
>>>>>> a
['a', 'b', 'c']
>>>>>> b = tuple(a)
>>>>>> b
('a', 'b', 'c')('a', 'b', 'c')
>>>>>> c = list(b)
>>>>>> c
['a', 'b', 'c']
>>>>>>
>>>>>> a = ['a', 'b', 'c']
>>>>>> a
['a', 'b', 'c']
>>>>>> b = tuple(a)
>>>>>> b
('a', 'b', 'c')('a', 'b', 'c')
>>>>>> c = list(b)
>>>>>> c
['a', 'b', 'c']
>>>>>>
这两个函数接受字符串参数时候比较有意思。
Code
>>>>>> list("xwy2.com")
['x', 'w', 'y', '2', '.', 'c', 'o', 'm']
>>>>>> tuple("xwy2.com")
>>>>>> list("xwy2.com")
['x', 'w', 'y', '2', '.', 'c', 'o', 'm']
>>>>>> tuple("xwy2.com")
2. 运算符操作
列表支持运算符操作。
Code
>>>>>> [1, 2] * 2
[1, 2, 1, 2]
>>>>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>>>>>
>>>>>> [1, 2] * 2
[1, 2, 1, 2]
>>>>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>>>>>
3. in / not in
可以使用 in / not in 来判断是否包含某个元素。
Code
>>>>>> a = [1, 2, 3]
>>>>>> 1 in a
True
>>>>>> 4 in a
False
>>>>>> b = (1, 2, 3)
>>>>>> 2 in b
True
>>>>>>
>>>>>> a = [1, 2, 3]
>>>>>> 1 in a
True
>>>>>> 4 in a
False
>>>>>> b = (1, 2, 3)
>>>>>> 2 in b
True
>>>>>>
4. range()
我们还可以使用 range() 函数获得一个整数列表,甚至进行运算和添加过滤条件。
Code
>>>>>> 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]
>>>>>>
>>>>>> 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]
>>>>>>
5. Slices
和字符串一样,我们可以通过序号或切片进行访问。
Code
>>>>>> 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,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]
>>>>>>
6. 相关方法
基本方法:
Code
>>>>>> 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 = ['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']]
>>>>>>
还可以使用 filter() 进行过滤。
Code
>>>>>> 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 = 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]
>>>>>>
上面的代码还可以简写为:
Code
>>>>>> filter(lambda i: i%2==0, range(10))
[0, 2, 4, 6, 8]
>>>>>>
>>>>>> filter(lambda i: i%2==0, range(10))
[0, 2, 4, 6, 8]
>>>>>>
当 function 参数(第一个参数)为 None 时,可以用来过滤掉空值。
Code
>>>>>> b = ['a', '', [], [1,2]]
>>>>>> filter(None,b)
['a', [1, 2]]
>>>>>>
>>>>>> b = ['a', '', [], [1,2]]
>>>>>> filter(None,b)
['a', [1, 2]]
>>>>>>
map() 类似 .NET 中的 Array.Foreach()
Code
>>>>>> map(lambda i:i*2, range(10))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>>>>
>>>>>> map(lambda i:i*2, range(10))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>>>>
另外,我们还可以使用 reduce() 对元素进行统计。
Code
>>>>>> import operator
>>>>>> reduce(operator.add, range(10))
45
>>>>>> reduce(operator.sub, [100, 5, 7])
88
>>>>>>
>>>>>> import operator
>>>>>> reduce(operator.add, range(10))
45
>>>>>> reduce(operator.sub, [100, 5, 7])
88
>>>>>>
zip() 方法可以对两个或多个列表/元组进行交叉合并。
Code
>>>>>> zip(range(2,10), ('a', 'b', 'c', 'd', 'e'))
[(2, 'a'), (3, 'b'), (4, 'c'), (5, 'd'), (6, 'e')]
>>>>>>
>>>>>> zip(range(2,10), ('a', 'b', 'c', 'd', 'e'))
[(2, 'a'), (3, 'b'), (4, 'c'), (5, 'd'), (6, 'e')]
>>>>>>