Fluent Python
Fluent Python
collections
list / array
Create array with fixed size and value
empty_array = [None] * 5 checked_array = [False] * 10
string to list
string to array of char
>>> s = 'abcde'
>>> list(s)
['a', 'b', 'c', 'd', 'e']
remove / print
remove item from array
>>> a
['a', 'b', 'c', 'd', 'e']
>>> a.remove('c')
>>> a
['a', 'b', 'd', 'e']
filter, map, reduce
filter
>>> l = [1, 3, 5, 7, 9]
>>> filter(lambda i: i % 3 == 0, l)
[3, 9]
map
>>> l
[1, 3, 5, 7, 9]
>>> map(lambda i: i * 2, l)
[2, 6, 10, 14, 18]
reduce
>>> l
[1, 3, 5, 7, 9]
>>> reduce(lambda a, b: a + b, l)
25
>>> reduce(lambda a, b: a if a > b else b, l)
9
print array of numbers to one line string
>>> a=[1, 2, 3]
>>> print a
[1, 2, 3]
>>> for i in a:
... print i,
...
1 2 3
>>> print ' '.join(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> print ' '.join(map(str, a))
1 2 3
set
>>> s1 = set('abc')
>>> s2 = set(['a', 'c', 'e'])
>>> s1.add('f')
>>> s1
set(['a', 'c', 'b', 'f'])
>>> s2
set(['a', 'c', 'e'])
>>> s1.update(['x', 'y'])
>>> s1
set(['a', 'c', 'b', 'f', 'y', 'x'])
>>> s1
set(['a', 'c', 'b', 'f', 'y', 'x'])
>>> s2
set(['a', 'c', 'e'])
>>>
>>> s1 | s2
set(['a', 'c', 'b', 'e', 'f', 'y', 'x'])
>>>
>>> s1 & s2
set(['a', 'c'])
>>>
>>> s1 - s2
set(['y', 'x', 'b', 'f'])
>>>
>>> s2 - s1
set(['e'])
String
char - lowercase / uppercase
def swap_case(input):
output = []
for c in input:
output.append(str.upper(c) if c.islower() else str.lower(c))
return''.join(output)
String to int
don’t need to do s.strip()
>>> int(' 1 ')
1
ASCII value of char
>>> ord('a')
97
>>> chr(97)
'a'
checking & chaning upper/lower case
>>> str.islower('a')
True
>>> str.isupper('Z')
True
>>> str.isupper('z'.upper())
True
>>> str.islower('A'.lower())
True
math
python division
>>> 4/3
1
>>> 4/3.0
1.3333333333333333
>>> 4/float(3)
1.3333333333333333
>>>
>>> from __future__ import division
>>> 4/3
1.3333333333333333
>>> 4//3
1
- 2016-03-16
edit