How to write Pythonic codes.
squares = []
for x in range(10):
squares.append(x**2)
squares
is equivalent to
squares = [x**2 for x in range(10)]
More complicated,
combs = []
for x in [1,2,3]:
for y in [3,4]:
if x != y:
combs.append((x, y))
combs
can be one-lined to
[(x, y) for x in [1,2,3] for y in [3,4] if x != y]
We can also use itertools:
[(x, y) for x, y in itertools.product(*[[1,2,3], [3,4]])]
where (*[A, B]) results in (A, B).
Sometimes we may want to use numpy.npindex:
[(x, y) for x, y in numpy.npindex((3, 4))]
which is the same to
out = []
for x in range(3):
for y in range(4):
out.append((x, y))
Before print the figure to PDF, make all words and plots fit:
plt.tight_layout(pad=0, h_pad=0, w_pad=0)
Use zip to iterate multiple lists at the same time.
alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']
for a, b in zip(alist, blist):
print(a, b)
An elegant way (from
here)
to generate n-gram using zip is
input_list = ['thisisasentencewithoutspace']
def find_ngrams(input_list, n):
return zip(*[input_list[i:] for i in range(n)])
Run rm /usr/local/bin/pip* and then apt-get install python-pip.
if __name__ == '__main__'means this module is called as main.
Use pyenv and pipenv. For example,
- Install an alternative Python:
env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.5.5. - Create a virtual envirnment at the workspace:
pipenv --python 3.5.pipenvwill work withpyenvto find where the required Python executable is. - Verify it:
pipenv shellandpython3 -V.