forked from childe/simplepython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl.py
More file actions
68 lines (51 loc) · 1.69 KB
/
l.py
File metadata and controls
68 lines (51 loc) · 1.69 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print "all operations of a list:"
#append, count, extend, index, insert, pop, remove, reverse, sort
print ", ".join([e for e in dir(list) if not e.startswith('_')])
l = [] # define a empty list
print type(l), l, len(l) # <type 'list'> [] 0
l = [1,2,3,4,5]
print type(l), l, len(l) # <type 'list'> [1, 2, 3, 4, 5] 5
l = range(10)
print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print "there is %d elements in l" % len(l)
# 切片, 和字符串一样
print l[1:2] # [1]
print l[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# 遍历
for e in l:
print e
print l.count(0) # 1
print l.index(0) # 0
# print l.index(100) #Error
#==================== 排序 ====================
l.reverse() # 反过来, 效果相当于l=l[::-1]
print l # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
l.sort() # 从小到大排序
print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l.sort(reverse=True) # 倒排, 从大到小
print l # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
l.sort(cmp=lambda x, y: x-y) # 这个应该是默认
print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l.sort(cmp=lambda x, y: y-x) # 相当于l.sort(reverse=True)
print l # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#==================== 添加删除修改 ====================
l = range(5)
l.append(100)
print l # [0, 1, 2, 3, 4, 100]
l.extend([40, 50])
print l # [0, 1, 2, 3, 4, 100, 40, 50]
l.insert(0, 3.14)
print l # [3.14, 0, 1, 2, 3, 4, 100, 40, 50]
l.insert(2, "second") # [3.14, 0, 'second', 1, 2, 3, 4, 100, 40, 50]
print l
l = [1, 1, 1, 2, 2, 3]
l.remove(2) # 只删除第一个
print l # [1, 1, 1, 2, 3]
# l.remove(10) #Error
p = l.pop() # 删除并返回最后一个
print p # 3
print l # [1, 1, 1, 2]
l[-1] = 100
print l # [1, 1, 1, 100]