= 1, 2, 3, 4, 5
values *rest = values
a, b, print(a)
print(b)
print(*rest)
1
2
3 4 5
statsmodel
library.some_function?
or ?some_function
, you can access the docstring*rest
is related to the **kwargs
= 1, 2, 3, 4, 5
values *rest = values
a, b, print(a)
print(b)
print(*rest)
1
2
3 4 5
append
, insert
, pop
, remove
, extend
, sort
insert
is computationally expensive compared with append
, because references to subsequent elements have to be shifted internally to make room for the new element. If you need to insert elements at both the beginning and end of a sequence, you may wish to explore collections.deque
, a double-ended queue, which is optimized for this purpose and found in the Python Standard Library.x in list
, x not in list
syntax
extend
to append elements to an existing list, especially if you are building up a large list, is usually preferable.sort
has a key
argument:= ["saw", "small", "He", "foxes", "six"]
b =len)
b.sort(key b
['He', 'saw', 'six', 'small', 'foxes']
del
, pop
, update
(merge 2 dicts),# Way 1
mapping = {}
for key, value in zip(key_list, value_list):
mapping[key] = value
# Way 2
= zip(range(5), reversed(range(5)))
tuples = dict(tuples)
mapping mapping
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
setdefault
is a thing
defaultdict
from collections
modulekeys
can be tuples as well as scalars (int, string, float)
a.difference_update(b)
& a.symmetric_difference(b)
zip
pairs up elements of sequences (lists, tuples) to create a list of tuples:= ['a', 'b', 'c']
s1 = [1, 2, 3]
s2 list(zip(s1, s2))
[('a', 1), ('b', 2), ('c', 3)]
list
comprehension, there’s also dict
and tuple
comprehension:= ["a", "as", "bat", "car", "dove", "python"]
strings # list
print([x.upper() for x in strings if len(x) > 2])
# tuple
print({len(x) for x in strings})
# dict
print({value: index for index, value in enumerate(strings)})
['BAT', 'CAR', 'DOVE', 'PYTHON']
{1, 2, 3, 4, 6}
{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}
As a rule of thumb, if you anticipate needing to repeat the same or very similar code more than once, it may be worth writing a reusable function. Functions can also help make your code more readable by giving a name to a group of Python statements.
global
and nonlocal
keywords for assigning variable inside a function to higher namespacesimport re
def clean_strings(strings):
= []
result for value in strings:
= value.strip()
value = re.sub("[!#?]", "", value)
value = value.title()
value
result.append(value)return result
= [" Alabama ", "Georgia!", "Georgia",
states "georgia", "FlOrIda","south carolina##",
"West virginia?"]
clean_strings(states)
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
# alternative
def remove_punctuation(value):
return re.sub("[!#?]", "", value)
# all the ops you do on a string added to this list
= [str.strip, remove_punctuation, str.title]
clean_ops
def clean_strings_alt(strings, ops):
= []
result for value in strings:
for func in ops:
# apply those functions on your value
= func(value)
value
result.append(value)return result
clean_strings_alt(states, clean_ops)
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
map
is interesting
map
applies a function to a sequenceprint(strings)
set(map(len, strings))
['a', 'as', 'bat', 'car', 'dove', 'python']
{1, 2, 3, 4, 6}
print(states)
list(map(remove_punctuation, states))
[' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda', 'south carolina##', 'West virginia?']
[' Alabama ',
'Georgia',
'Georgia',
'georgia',
'FlOrIda',
'south carolina',
'West virginia']
def apply_to_list(some_list, f):
return [f(x) for x in some_list]
= [4, 0, 1, 5, 6]
ints
lambda x: x * 2) apply_to_list(ints,
[8, 0, 2, 10, 12]
= ["foo", "card", "bar", "aaaa", "abab"]
strings
strings.sort() strings
['aaaa', 'abab', 'bar', 'card', 'foo']
= ["foo", "card", "bar", "aaaa", "abab"]
strings =lambda x: len(set(x))) #sort by num of unique chars in str
strings.sort(key strings
['aaaa', 'foo', 'abab', 'bar', 'card']
itertools
module
groupby
, chain(*iterables)
, combinations(iterable, k)
, permutations(iterable, k)
, groupby(iterable[, keyfunc])
, product(*iterables, repeat=1)
try/except
is for when you want things to fail gracefullyfloat("something")
ValueError: ignored
def attempt_float(x):
try:
return float(x)
except:
return x
"something") attempt_float(
'something'
# there are other types of error
# e.g. type error
float((1, 2))
TypeError: ignored
# might want to specify which error type to except, therefore:
def attempt_float(x):
try:
return float(x)
except ValueError:
return x
# or a tuple of errors
def attempt_float(x):
try:
return float(x)
except (TypeError, ValueError):
return x
finally
is for the final things that must happen regardless of what happens with try
:f = open(path, mode="w")
try:
write_to_file(f)
finally:
f.close()
else
is for what happens if try
succeeded:f = open(path, mode="w")
try:
write_to_file(f)
except:
print("Failed")
else:
print("Succeeded")
finally:
f.close()
import pandas as pd
import numpy as np
= pd.DataFrame(np.arange(9).reshape((3, 3)),
frame =["a", "c", "d"],
index=["Ohio", "Texas", "California"])
columns frame
Ohio | Texas | California | |
---|---|---|---|
a | 0 | 1 | 2 |
c | 3 | 4 | 5 |
d | 6 | 7 | 8 |
# this exists
"a", "c"], ["California", "Texas"]] frame.loc[[
California | Texas | |
---|---|---|
a | 2 | 1 |
c | 5 | 4 |