-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_jump_zip_*.py
143 lines (92 loc) · 2.39 KB
/
list_jump_zip_*.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def fact(n):
if n == 1:
return 1
return n * fact(n - 1)
print(fact(3))
##递推函数
def power(x, n=2):
s = 1
while n > 0:
s = s * x
n = n - 1 # n只起到决定决定循环次数的作用
return s # why the result will be different if the place if return change
print(power(5))
def greet(name='world'):
print('hello', name)
print(greet())
print(greet('bob'))
def fn(*args):
print(args)
def mean(*num): # * 号表示1-无尽个数
if len(num) > 0:
return sum(num) * 1.0 / len(num)
else:
return 0
print(mean(1, 3, 5))
L = (1, 4, 5)
SUM = 0
for x in L:
SUM = SUM + x
print(SUM)
L = ['ada', 'lisa', 'bart', 'paul']
print(L[::2])
# L[::] 历遍 L[::2] loop and jump when every two objects
M = list(range(0, 101))
print(M)
print(M[1:10])
print(M[::3]) # jump when every three objects
print(M[:50:5]) # only for first 50 num and jump when every five objects
print(M[-10:])
print(M[-46::5])
'abc'.upper()
L = list(range(1, 101))
for i in L:
if i % 7 == 0: # i % 7==0 means remiander is 0
print(i)
list1 ,list2 =[1,7,9],[3,10,6]
data = zip(list1, list2)
print(data)
data = sorted(data,key=lambda x: x[1])
print(data)
[(1, 3), (9, 6), (7, 10)]
list1, list2 = map(lambda t: list(t), zip(*data))
print(list1)
[1, 9, 7]
print(list2)
[3,6,10]
d = {'a', 'b', 'c'}
for key in d:
print(key)
for i, key in enumerate(['a', 'b', 'c']):
print(i, key)
L = ['a', 'b', 'c', 'd']
M = [1, 2, 3, 4]
S = zip(M, L)
# zip object when need to be print out --- use list(S)
# zip like stapler
array = [['a', 'b'], ['c', 'd'], ['e', 'f']]
transposed = list(zip(*array))
print(transposed)
b = ["red", "green", "blue"]
f = ["strawberry", "kiwi", "blueberry"]
# simply think that zip is transpose the list
print(dict(zip(b, f)))
####### F(x)
def _zip(*sequence):
return list(map(list,zip(*sequence)))
# transfer two list to dict ---think it like transpose
person= collections.namedtuple('Person','name age height')
jane = person('Jane',25,1.75)
print(jane)
a=jane._asdict()
print(a)
for key, value in a.items():
print(key + ':' ,value)
for field, value in zip(jane._fields, jane):
print(field,'->', value)
a = ("John", "Charles", "Mike")
b = ("Jenny", "Christy", "Monica", "Vicky")
x = zip(a, b)
print(list(x))
for field, value in zip(a, b):
print(field, 'marrys', value)