1. 列表操作
集合的特征
可随意增加
可以删除
可修改
可索引
可遍历
list列表
定义:用[]包含的一些元素
如:study_list[0, 'xxx', None, True,[],[2]]
定义一个list
>>> arr = ['C', 'python', 'js', 'css', 'html', 'node'] >>> arr ['C', 'python', 'js', 'css', 'html', 'node'] >>> arr[0] 'C' >>> arr[1] 'python' >>> arr[-1] 'node' >>> arr[-3] 'css'
访问列表:索引,从左到右依次编号为0,1,2,3,4...,从右到左依次编号为-1,-2,-3...
>>> num_list = range(0,100) >>> num_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> num_list[-2] 98
range(start,end,step)
>>> for x in 'abc':
... print x
...
a
b
c
>>> list('abc')
['a', 'b', 'c']
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(0,10,2)
[0, 2, 4, 6, 8]
>>> range(0,10,3)
[0, 3, 6, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]
in:判断成员是否在列表中
>>> 'an' in ['anzhihe', 'haha'] False >>> 'an' in ['an', 'haha'] True
函数(len|max|min|del)
>>> arr = [1, 2, 4, 10, -2, -8, 110, 119, 502] >>> len(arr) #长度 9 >>> max(arr) #最大值 502 >>> min(arr) #最小值 -8 >>> arr [1, 2, 4, 10, -2, -8, 110, 119, 502] >>> arr[0] = 'anzhihe' #修改列表元素 >>> arr ['anzhihe', 2, 4, 10, -2, -8, 110, 119, 502] >>> del arr[0] #删除列表元素 >>> arr [2, 4, 10, -2, -8, 110, 119, 502]
切片:从list获取一部分元素组成新的list(list_name[start:end:step])
>>> num_list = range(0,10) >>> num_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> num_list[::2] #获取偶数 [0, 2, 4, 6, 8] >>> num_list[3:6] #获取3,4,5 [3, 4, 5]
赋值方式:传值,传址
添加一个元素:names[idx:idx] = [a,b]
删除某个元素:names[idx:idx+1] = [], names[idx:idx+n] = []
修改某个元素:names[idx:idx+1] = [xxx]
列表运算
#列表相加 >>> names1 = ['anzhihe', 'hehe', 'haha'] >>> names2 = ['azh', 'xixi', 'xy'] >>> names1 + names2 ['anzhihe', 'hehe', 'haha', 'azh', 'xixi', 'xy'] #列表相乘(用于初始化) >>> names1 * 2 ['anzhihe', 'hehe', 'haha', 'anzhihe', 'hehe', 'haha'] >>> [0] * 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
2. 列表方法
append 追加元素
append(...)
L.append(object) -- append object to end
>>> names1
['anzhihe', 'hehe', 'haha']
>>> names1.append('append')
>>> names1
['anzhihe', 'hehe', 'haha', 'append']
count 统计次数
count(...)
L.count(value) -> integer -- return number of occurrences of value
>>> chars = ['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w',123, 1, 2]
>>> chars.count('a')
1
>>> chars.count('c')
2
练习1:求列表里字符出现的次数
chars = ['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w',123, 1, 2]
char = raw_input("please input a char:")
count = 0
for c in chars:
if c == char:
count += 1
print "%s:%d" % (char, count)
extend 扩展原列表
extend(...)
L.extend(iterable) -- extend list by appending elements from the iterable
>>> chars
['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w', 123, 1, 2]
>>> chars.extend('xyz')
>>> chars
['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w', 123, 1, 2, 'x', 'y', 'z']
>>> chars.append('xyz')
>>> chars
['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w', 123, 1, 2, 'x', 'y', 'z', 'xyz']
>>> chars.extend(['mm', 'gg'])
>>> chars
['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w', 123, 1, 2, 'x', 'y', 'z', 'xyz', 'mm', 'gg']
>>> chars.append(['mm', 'gg'])
>>> chars
['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w', 123, 1, 2, 'x', 'y', 'z', 'xyz', 'mm', 'gg', ['mm', 'gg']]
index 获取索引
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
>>> chars
['a', 'b', 'c', 'c', 'e', 'd', 'b', 'z', 'u', 'w', 123, 1, 2, 'x', 'y', 'z', 'xyz', 'mm', 'gg', ['mm', 'gg']]
>>> chars.index('b')
1
>>>
>>> chars.index('c')
2
>>> chars.index('c',2,3)
2
>>> chars.index('c',3,4)
3
>>> chars.index('c',4,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list
#元素不在列表中会报错
insert 插入元素
insert(...) L.insert(index, object) -- insert object before index >>> chars_list = ['a'] >>> chars_list.insert(0, 'b') >>> chars_list ['b', 'a'] >>> chars_list.insert(10, 'c') >>> chars_list ['b', 'a', 'c'] >>> chars_list.insert(2,'d') >>> chars_list ['b', 'a', 'd', 'c'] >>> chars_list.insert(-1,'e') >>> chars_list ['b', 'a', 'd', 'e', 'c'] >>> chars_list.insert(-3,'f') >>> chars_list ['b', 'a', 'f', 'd', 'e', 'c']
pop remove 移出一个元素
pop(...)
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
>>> chars_list
['b', 'a', 'f', 'd', 'e', 'c']
>>> chars_list.pop()
'c'
>>> chars_list
['b', 'a', 'f', 'd', 'e']
>>> chars_list.pop(1)
'a'
>>> chars_list
['b', 'f', 'd', 'e']
>>> help(list.remove)
Help on method_descriptor:
remove(...)
L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.
>>> chars_list
['b', 'f', 'd', 'e']
>>> chars_list.remove('f')
>>> chars_list
['b', 'd', 'e']
>>> chars_list.remove('w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
reverse 反向存储
reverse(...) L.reverse() -- reverse *IN PLACE* >>> chars_list ['b', 'd', 'e'] >>> chars_list.reverse() >>> chars_list ['e', 'd', 'b']