Python: Array v. List

Use lists unless you want some very specific features that are in the C array libraries. python really has three primitive data structures More on data structures here: http://docs.python.org/tutorial/datastructures.html

Union of two lists in Python

Perform a union, keeping repetition: Perform a union, keeping repetition & order: Perform an union, no repetition in each list, but repetition allowed in final union, and keeped order: After clarification of the question, the goal is to build a list that take elements (including repetition) of, and then add elements of b if they … Read more

Embedding in pytorch

nn.Embedding holds a Tensor of dimension (vocab_size, vector_size), i.e. of the size of the vocabulary x the dimension of each vector embedding, and a method that does the lookup. When you create an embedding layer, the Tensor is initialised randomly. It is only when you train it when this similarity between similar words should appear. Unless you … Read more

How do you read from stdin?

You could use the fileinput module: fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided. Note: line will contain a trailing newline; to remove it use line.rstrip()

How do I install the yaml package for Python?

You could try the search feature in pip, which looks for packages in PyPI with yaml in the short description. That reveals various packages, including PyYaml, yamltools, and PySyck, among others (Note that PySyck docs recommend using PyYaml, since syck is out of date). Now you know a specific package name, you can install it: If you … Read more

Changing the “tick frequency” on x or y axis in matplotlib?

You could explicitly set where you want to tick marks with plt.xticks: For example, (np.arange was used rather than Python’s range function just in case min(x) and max(x) are floats instead of ints.) The plt.plot (or ax.plot) function will automatically set default x and y limits. If you wish to keep those limits, and just change the stepsize of the tick marks, then you could use ax.get_xlim() to discover what limits Matplotlib … Read more

Why am I getting this syntax error: keyword can’t be an expression

You are defining a keyword argument by using =: That makes everything before the = a Python expression, namely number_1 * number_2. Python doesn’t allow this. If you wanted to print out a nicely formatted expression, you’ll have to use separate string arguments: Python writes out separate arguments with a space to separate them (but you can use sep=’…’ to set a different … Read more