forked from kbroman/ProgrammingNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.txt
More file actions
411 lines (320 loc) · 9.41 KB
/
python.txt
File metadata and controls
411 lines (320 loc) · 9.41 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
Notes on Python
Install anaconda from http://continuum.io/downloads
I installed it in the default location (/anaconda)
Install python 3
conda create -n py3 python=3 anaconda
conda create -n py27 python=2.7 anaconda
---
Switching to python 3 (and back)
source activate py3
source deactivate
---
Remove environment
conda env remove -n py33
---
IDE with anaconda is called "Spyder"
(standard IDE is "idle" or "IDLE" but on my Mac it uses python2)
(IDLE3 is the python3 version)
---
With ipython, use %paste to paste multiple lines all at once (and
avoid identation issues)
------------------------------
better colors for ipython:
create ~/.ipython/profile_default/ipython_config.py
with:
c = get_config()
c.TerminalInteractiveShell.colors = 'Linux'
------------------------------
dir("blah") gives list of relevant methods
(e.g., blah = str)
# duplicating arrays
a = [1,2,3]
b = a # completely linked
c = a*1 # duplicate
a is b
a is not c
------------------------------
Adding to lists
x = [2, -5, 3, 1, -3]
x.append(3)
x.extend([3,4])
------------------------------
Dictionaries
x = {"name": "karl", "age": "really_old", "shoe_size": 8}
x["shoe_size"]
x.keys()
x.values()
list(x.keys())
list(x.values())
------------------------------
Joining list elements into a string
a bit odd to me: separator.join(list)
x = ["a", "b", "c"]
",".join(x)
------------------------------
See also:
https://github.com/kbroman/PyBroman/blob/master/ParseGenotypeData/convert3.py
------------------------------
# null value
x = None
# loop
for i in range(10):
print(i)
------------------------
Jupyter notebooks
type "jupyter notebook" at shell prompt
then click New -> Python 3
within jupyter: type '%whos' to see what variables you've assigned,
like ls() in R
------------------------
numpy
x = numpy.array([[1,2],[3,4],[5,6],[7,8]])
numpy.shape(x) # tuple (immutable) with (n_rows, n_cols)
x[0,0]
x[:,1]
x[2,:]
------------------------
matplotlib
import matplotlib.pyplot as plt
plt.figure()
x = [xv+1 for xv in range(6)]
y = [xv**2 for xv in x]
plt.xlabel("X")
plt.ylabel("Y")
plt.title("first matplotlib plot")
plt.plot(x, y)
plt.show()
######################################################################
# various by python (v3.3) stuff while I learn the langugage
# (taking my features.rb script and porting it to python)
# Really useful to me:
# google course on python: https://developers.google.com/edu/python/
# software carpentry bootcamp: http://software-carpentry.org/v4/python/index.html
# loops
for x in range(1, 10):
print(x, end="")
print()
for x in range(9, 4, -1):
print(x, end="")
print()
for x in range(0, 21, 5):
print(x, end=" ")
print()
# string methods
print('Length of "This is a test"', end="")
print(len('This is a test'))
print('This is a test'.lower())
print('This is a test'.upper())
print('This is a test'.swapcase())
# reverse ("extended slice syntax": begin:end:step)
print("This is a test"[::-1])
# string manipulation & reg ex
import re
# sub just the first
print(re.sub('bar', 'foo', 'foobarfoobar', count=1))
# global sub
print(re.sub('bar','foo','foobarfoobar'))
print(re.sub(r'\s+', '|', "This is a test", count=1))
print(re.sub(r'\s+', '|', 'This is a test'))
text = '<h3 align="center">Popularity in 1990</h3>\n'
year = re.search(r'Popularity in (\d\d\d\d)', text).group(1)
print(year)
text = '<tr align="right"><td>7</td><td>Andrew</td><td>Stephanie</td>'
rank_and_names = re.search(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', text)
print(rank_and_names.group())
print(rank_and_names.group(1))
print(rank_and_names.group(2))
print(rank_and_names.group(3))
rank_and_names = re.findall(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', text)
print(rank_and_names)
text2 = '''<tr align="right"><td>7</td><td>Andrew</td><td>Stephanie</td>
<tr align="right"><td>8</td><td>James</td><td>Jennifer</td>
<tr align="right"><td>9</td><td>Justin</td><td>Elizabeth</td>'''
rank_and_names = re.findall(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', text2)
print(rank_and_names)
# split on whitespace
print("Blah blah blah. ".split())
# arrays (lists)
arr = [1, "test", 2, 3, 4]
for x in arr:
print(str(x) + "X ", end="")
print()
# formated print
for x in arr:
print("%sX " % x, end="")
print()
# map
x = list(map(lambda x:x+1, range(6)))
print(x)
# list comprehension
x = [y+1 for y in range(6)]
print(x)
y = list(map(lambda x:x+2, range(6)))
print(' '.join(map(str, y)))
# ranges
x = list(range(6))
y = list(range(1, 7))
z = list(range(3, 50, 5))
print(x, y, z)
# loops
for i in range(1,6):
print("%d^2 = %d" % (i, i**2))
i = 1
while i <= 5:
print("%d^2 = %d" % (i, i**2))
i += 1
# other array methods
x = list(range(1,6))
y = [2, 4, 1]
print(x+y)
print(":".join(map(str, x+y)))
# aliasing
x = list(range(1,6))
y = [2, 4, 1]
z = x # aliased
zz = list(x) # a copy
id(x) == id(z) # True
id(x) != id(zz) # True
for yy in y:
if yy in x: x.remove(yy)
print(x, z, zz)
print(3 in x)
print(7 in x)
print(x[0]) # first element
print(x[-1]) # last element
z = range(5, 9)
print(z[-2:]) # a range
z = list(z)
print(z[-2:]) # now a list
zz = z.reverse() # doesn't return
print(z, zz) # zz = None
zz = reversed(z)
print(zz) # an iterator
zz = list(reversed(z))
print(zz) # a list
# hashes (hash is called a 'dict')
x = {"a" : 1, "b" : 2, "c" : 3}
print(x['a'])
for (value,key) in x.items()
print(key, ' -> ', value)
print(list(x.keys()))
x.pop("a")
print(x)
x = {"a" : 1, "b" : 2, "c" : 3}
z = list(x.keys()) # need list() since I'll be modifying the keys in place
for key in z: # "for key in x:" would work if I weren't modifying the keys in place
if x[key] == 2:
x.pop(key)
print(x)
# alternatively:
x = {"a" : 1, "b" : 2, "c" : 3}
z = [key for key in x.keys() if x[key] == 2]
z = [key for key in x if x[key] == 2] # equivalently
for key in z:
x.pop(key)
print(x)
x = {"a":1, "b":2}
x['d'] = x['d']+1 if 'd' in x else 1
## slices of arrays, negative index to start from end
a = list(range(2, 13, 2))
print(a[1:3])
print(a[-1])
print(a[-3:-2])
print(a[-3])
print(a[-3:-1])
## conversion between classes
int("5") # to integer
float("6") # to float
str(252.3) # to string
## a bit of text manipulation
text = '''We may at once admit that any inference from the particular
to the general must be attended with some degree of uncertainty,
but this is not the same as to admit that such inference cannot
be absolutely rigorous, for the nature and degree of the uncertainty
may itself be capable of rigorous expression.'''
stopwords = 'the a by on for of are with just but and to my in I has some'.lower().split()
words = text.lower().split()
keywords = [word for word in words if word not in stopwords]
print(' '.join(keywords))
print("no. char =", len(' '.join(keywords)))
print("no. words =", len(keywords))
## playing with map
n = 8
counts = map(lambda x: 0, range(n))
print(' '.join(map(str, counts)))
import random
x = map(lambda z: random.randint(1,8), range(1000))
counts = []
for i in range(1,9):
counts.append( sum(z==i for z in y) )
print(' '.join(map(str, counts)))
## looping over hashes (also sorting)
words = '''We may at once admit that any inference from the particular to the general
must be attended with some degree of uncertainty, but this is not the same as to
admit that such inference cannot be absolutely rigorous, for the nature and
degree of the uncertainty may itself be capable of rigorous expression.'''.split()
import re
words = list(map(lambda word: re.sub(r'[,\.]', '', word), words))
wordcount = {}
for word in words:
wordcount[word] = wordcount[word]+1 if word in wordcount else 1
# sort by word length
sorted(wordcount.keys(), key=len)
# sort by count
sorted(wordcount.keys(), key=lambda x: wordcount[x])
# by count then word length
sorted(wordcount.keys(), key=lambda x: [wordcount[x], len(x)])
# by word length then count
sorted(wordcount.keys(), key=lambda x: [len(x), wordcount[x]])
# by count then word length, but reversed
sorted(wordcount.keys(), key=lambda x: [wordcount[x], len(x)], reverse=True)
# using a function
def count_and_length (a):
return [wordcount[a], len(a)]
sorted(wordcount.keys(), key=count_and_length)
## regex
import re
if not re.search(r'AM', 'am'):
print('ok 1')
if re.search(r'(?i)AM', 'am'):
print('ok 2')
if re.search(r'AM', 'am', re.IGNORECASE):
print('ok 3')
multi = 'blah a number of special\nAll of these are'
if re.search(r'\Ablah', multi):
print('ok 4')
if not re.search(r'\AAll', multi):
print('ok 5')
if re.search(r'^blah', multi):
print('ok 6')
if not re.search(r'^All', multi):
print('ok 7')
if re.search(r'^A', multi, re.MULTILINE):
print('ok 8')
if not re.search(r'special\Z', multi):
print('ok 9')
if re.search(r'special$', multi, re.MULTILINE):
print('ok 10')
if re.search(r'are\Z', multi, re.MULTILINE):
print('ok 11')
if re.search(r'are\Z', multi):
print('ok 12')
if not re.search(r'special$', multi):
print('ok 13')
if re.search(r'special$', multi, re.MULTILINE):
print('ok 14')
if re.search(r'are$', multi):
print('ok 15')
if not re.search(r'blah.*are', multi):
print('ok 16')
if re.search(r'blah.*are', multi, re.DOTALL):
print('ok 17')
x = 'Today is 11/26/2013, while tomorrow is 11/27/2013.'
z = re.search(r'(\d+)/(\d+)/(\d+)', x)
if z:
print('Month = %s, day = %s, year = %s' % (z.group(1), z.group(2), z.group(3)))
zz = re.findall(r'(\d+)/(\d+)/(\d+)', x)
if zz:
print('Month = %s, day = %s, year = %s' % (zz[0][0], zz[0][1], zz[0][2]))
if len(zz) > 1:
print('Month = %s, day = %s, year = %s' % (zz[1][0], zz[1][1], zz[1][2]))