-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlist.py
More file actions
210 lines (173 loc) · 5.56 KB
/
Copy pathlist.py
File metadata and controls
210 lines (173 loc) · 5.56 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
a = [1,2]
a.append(3)
a[4] # err
len(a) # 2
[1,2] == [1,2] # vals equal: True
[] is [] # identity: False (arrays have different locations in memory)
[3] is [3] # ...
[1,2] * 2 # list repeat: [1,2,1,2]
[ [1,2] ] * 2 # ...: [ [1,2], [1,2] ]
3 in [1,2,3] # containment: True O(N)
[*a] # list unpack: [1,2]
[*range(4)] # ...: [1,2,3,4]
[1,2] + [3,4] # list concat: [1,2,3,4]
[ *[1,2], *[3,4] ] # ...
[1,2]+[3]+[4] # ...
([1,2]+[3,4])[1:] # concat & slice: [2,3,4]
[1,2]+[3,4][1:] # ... [1,2,4]
[1,2].append(3) # NoneType, a: [1,2,3]
[1,2,3].insert(2,4) # NoneType, a: [1,2,4,3]
[1,2].extend([3,4]) # NoneType, a: [1,2,3,4]
[7,8,9].remove(8) # NoneType, a: [7,9]
[1,2,3].reverse() # NoneType, a: [3,2,1]
list(reversed([1,2,3])) # [3,2,1]
[*reversed([1,2,3])] # ...
[1,2,3][::-1] # ...
[3,1,2].sort() # NoneType, a: [1,2,3]
[1,2,3,3].count(3) # 2 O(N)
[1,2].index(2) # 1
[1,2].index(3) # err
[1,1,2].index(1,1) # 1
['a','b'].index('a') # 0
a = [3]
a.append(4) # [3,4]
a.pop() # 4, a: [3]
a.pop() # 3, a: []
a.clear() # []
a.copy()
# https://docs.python.org/3/tutorial/datastructures.html#more-on-lists
# sort
<list>.sort(key=None, reverse=False) | sorted(iterable, key=None, reverse=False)
a = [3,1,2]
a.sort()
a # [1,2,3]
sorted([3,1,2]) # [1,2,3]
sorted([3,1,2], reverse=True) # [3,2,1]
sorted(['A', 'B', 'a', 'b']) # ['A', 'a', 'B', 'b']
sorted(['1','10','11','2']) # ['1', '10', '11', '2']
sorted(['1','10','11','2'], key=int) # ['1', '2', '10', '11']
sorted(['c','B','a']) # ['B', 'a', 'c']
sorted(['c','B','a'], key=str.lower) # ['a', 'B', 'c']
sorted([(1,6), (2,5), (3,4)], key=lambda i: i[1]) # [ (3,4), (2,5), (1,6) ]
sorted([{'a':4}, {'a':3}, {'a':2}], key=lambda i: i['a']) # [ {'a':2}, {'a':3}, {'a':4} ]
sorted([ [1,2,3,4], [1,2,3], [1,2] ], key=lambda i: len(i)) # [ [1,2], [1,2,3], [1,2,3,4] ]
from operator import itemgetter, attrgetter
sorted([(1,6), (2,5), (3,4)], key=itemgetter(1)) # [ (3,4), (2,5), (1,6) ]
sorted([{'a':4}, {'a':3}, {'a':2}], key=attrgetter('a')) # [ {'a':2}, {'a':3}, {'a':4} ]
# sort by locale
ref = 'آ ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی'
a = ref.split(' ')
' '.join(sorted(a)) == ref # False (bad)
import locale
from functools import cmp_to_key
locale.setlocale(locale.LC_ALL, 'Persian') # or 'fa_FA.UTF-8'
' '.join(sorted(a, key=locale.strxfrm)) == ref # True (good, use this)
' '.join(sorted(a, key=cmp_to_key(locale.strcoll))) == ref # True (good)
locale.setlocale(locale.LC_ALL, '')
import icu # pip install PyICU (did not install correctly)
collator = icu.Collator.createInstance(icu.Locale('fa_FA.UTF-8'))
' '.join(sorted(a, key=collator.getSortKey))
# creation
a = range(1,10,2)
list(a) # [1,3,5,7,9]
# another concat
a = [1,2]
b = [3,4]
a += b
a # [1,2,3,4]
# index access
a = [1,2,3,4]
a[0] # 5
a[-1] # 8
# range slice
arr[?start=0: ?stop=-1: ?step=0]
a[0:2] # [1,2]
a[:2] # [1,2]
a[2:] # [3,4]
a[-3:-1] # [2,3]
a[:] # copy of arr
[1,2,3,4,5,6][::2] # [1,3,5]
[1,2,3,4,5,6][::-1] # [6,5,4,3,2,1]
# list comprehension
a = [1,2,3,4]
[i*i for i in a] # [1,4,9,16]
[i for i in a if i % 2 == 0] # [4,16]
[i if i % 2 == 0 else '' for i in a] # ['', 2, '', 4]
a = [ [1,2], [3,4] ]
[[j*2 for j in i] for i in a] # [ [2,4], [6,8] ]
# map
def add(n): return n + n
res = map(add, [1,2,3])
list(res) # [2,4,6]
list(map(lambda i: i*2, [1,2,3])) # [2,4,6]
# map - mutable
a = map(lambda i: i*2, [2,3])
list(a) # [4,6] (src obj gone)
list(a) # []
# map - over index & value
list( map(lambda i: i[0], enumerate([4,5,6])) ) # [0,1,2]
list( map(lambda i: i[0], enumerate([4,5,6], 7)) ) # [7,8,9]
# map - over index & value - workaround for unpacking a tuple param of lambda
list( map(lambda i: (x:=i[0], y:=i[1], x+y)[-1], enumerate([1,2,3])) ) # [1,3,5]
from collections import namedtuple
tup = namedtuple('tup', 'x,y')
list( map(lambda i: (t:=tup(*i), t.x+t.y)[-1], enumerate([1,2,3])) ) # [1,3,5]
# filter
list(filter(lambda i: i>2, [1,2,3,4])) # [3,4]
# reduce
from functools import reduce
reduce(lambda r,i: r+i, [1,2,3,4]) # 10
reduce(lambda r,i: r+i, [1,2,3,4], 5) # 15
# flat
from itertools import chain
a = [ [1,2], [3,4] ]
list(chain(*a)) # [1,2,3,4]
list(chain.from_iterable(a)) # ...
reduce(lambda x, y: x+y, a) # ...
a = map(lambda i: [i]*4, [1,2])
list(chain(*a)) # [1,1,1,1,2,2,2,2]
a = ...
list(chain.from_iterable(a)) # ...
[i for a in a for i in a] # ...
# remove duplicates
a = [1,1,2]
list(set(a)) # [1,2]
list(dict.fromkeys(a)) # ...
# pass-by-reference
a = [1,2,3,4]
b = a
b[0] = 57
a[0] # 57
b = [*a] # or a[:]
b[0] = 57
a[0] # 1
# ↑... muldim
a = [[1], [2]]
b = a[:]
b[0][0] = 7
a # [[7], [2]]
b # [[7], [2]]
# ↑... 1-level copy
a = [[1], [2]]
b = [i[:] for i in a]
b[0][0] = 7
a # [[1], [2]]
b # [[7], [2]]
# deep copy
import copy
a = [ [1], [[2]] ]
b = copy.deepcopy(a)
b[1][0][0] = 7
a # [ [1], [[2]] ]
b # [ [7], [[7]] ]
# iteration
a = [1,2,3]
b = [4,5,6]
for i in range(len(a)):
print(a[i], b[i]) # 1 4 2 5 3 6
for i, v in enumerate(a):
print(a[i], b[i]) # 1 4 2 5 3 6
for i in zip(a,b):
print(i) # (1,4) (2,5) (3,6)
for i in enumerate(zip(a,b)):
print(i) # (0,(1,4)) (1,(2,5)) (2,(3,6))