-
Notifications
You must be signed in to change notification settings - Fork 0
/
tips_tricks.py
136 lines (110 loc) · 3.08 KB
/
tips_tricks.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
# https://docs.python.org/3/library/functions.html#built-in-functions
# iterate: range() >> enumeration()
numbers = [45, 22, 14, 65, 97, 72]
for i in range(len(numbers)):
if numbers[i] % 3 == 0 and numbers[i] % 5 == 0:
numbers[i] = 'fizzbuzz'
elif numbers[i] % 3 == 0:
numbers[i] = 'fizz'
elif numbers[i] % 5 == 0:
numbers[i] = 'buzz'
for i, num in enumerate(numbers):
if num % 3 == 0 and num % 5 == 0:
numbers[i] = 'fizzbuzz'
elif num % 3 == 0:
numbers[i] = 'fizz'
elif num % 5 == 0:
numbers[i] = 'buzz'
numbers
# apply: map() and filter() >> list comprehension
numbers = [4, 2, 1, 6, 9, 7]
def square(x):
return x*x
list(map(square, numbers))
[square(x) for x in numbers]
def filter_is_odd(x):
return bool(x % 2)
list(filter(filter_is_odd, numbers))
[x for x in numbers if filter_is_odd(x)]
# sort: sort() >> sorted()
sorted(['cat', 'dog', 'cheetah', 'rhino', 'bear'], reverse=True)
animals = [
{'type': 'penguin', 'name': 'Stephanie', 'age': 8},
{'type': 'elephant', 'name': 'Devon', 'age': 3},
{'type': 'puma', 'name': 'Moe', 'age': 5},
]
sorted(animals, key=lambda animal: animal['age'])
# speed: list[] >> set()
import random
all_words = "all the words in the world".split()
def get_random_word():
return random.choice(all_words)
def get_unique_words():
words = []
for _ in range(1000):
words.append(get_random_word())
return set(words)
get_unique_words()
def get_unique_words():
words = set()
for _ in range(1000):
words.add(get_random_word())
return words
get_unique_words()
# memory saving: list[] >> generators()
# out of memory
sum([i * i for i in range(1, 10000001)])
# only one element exists in memory at a time
sum((i * i for i in range(1, 10000000001)))
# standard library
student_grades = {}
grades = [
('elliot', 91),
('neelam', 98),
('bianca', 81),
('elliot', 88),
]
for name, grade in grades:
if name not in student_grades:
student_grades[name] = []
student_grades[name].append(grade)
student_grades
# defaultdict
from collections import defaultdict
student_grades = defaultdict(list)
for name, grade in grades:
student_grades[name].append(grade)
student_grades
# Counter
from collections import Counter
words = "if there was there was but if there was not there was not".split()
counts = Counter(words)
counts
counts.most_common(2)
# string
import string
string.ascii_letters
string.ascii_uppercase
string.ascii_lowercase
string.digits
string.hexdigits
string.octdigits
string.punctuation
string.printable
string.whitespace
def is_upper(word):
for letter in word:
if letter not in string.ascii_uppercase:
return False
return True
is_upper('Thanks Geir')
False
is_upper('LOL')
True
import itertools
friends = ['Monique', 'Ashim', 'Devon', 'Bernie']
# list of every possible grouping of input values with two elements
# with permutations, the order of the elements matters
list(itertools.permutations(friends, r=2))
# with combinations, order is irrelevant
list(itertools.combinations(friends, r=2))