Skip to content

Latest commit

 

History

History
114 lines (77 loc) · 1.89 KB

File metadata and controls

114 lines (77 loc) · 1.89 KB

Python Notes

How to write Pythonic codes.

List

List Comprehensions

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))

Matplotlib

Before print the figure to PDF, make all words and plots fit:

plt.tight_layout(pad=0, h_pad=0, w_pad=0)

zip

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)])

pip

ImportError: No module named pip error

Run rm /usr/local/bin/pip* and then apt-get install python-pip.

Misc

  • if __name__ == '__main__' means this module is called as main.

Multiple versions coexisting

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. pipenv will work with pyenv to find where the required Python executable is.
  • Verify it: pipenv shell and python3 -V.