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
| #定义List
>>>li = ["a", "b", "mpilgrim", "z", "example"]
>>>li
["a", "b", "mpilgrim", "z", "example"]
>>>li[0]
'a'
>>>li[4]
'example'
>>>li[-1] #li[-n] = li[len[li]-n]
'example'
>>>li[-3]
'mpilgrim'
#添加元素
>>>li.append("cc")
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'cc']
#合并列表
>>> li.extend([1,2,3,4])
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'cc', 1, 2, 3, 4]
#删除元素
>>> del li[0]
>>> li
['b', 'mpilgrim', 'z', 'example', 'cc', 1, 2, 3, 4]
#分片(slice)
>>>li[1:3]
["b", "mpilgrim"]
>>> li.remove('b')
>>> li
['mpilgrim', 'z', 'example', 'cc', 1, 2, 3, 4]
#多列排序
>>>list = [{"name":"aa", "sort":1}, {"name":"bb", "sort":8}, {"name":"zz", "sort":5}, {"name":"ff", "sort":3}]
>>>from operator import itemgetter, attrgetter
>>>list.sort(key=itemgetter("sort", "name"), reverse=True)
>>>list
[{'sort': 8, 'name': 'bb'}, {'sort': 5, 'name': 'zz'}, {'sort': 3, 'name': 'ff'}, {'sort': 1, 'name': 'aa'}]
|