Skip to content

Prepend the same string to all items in a list

Have done some searching through Stack Exchange answered questions but have been unable to find what I am looking for. Given the following list: How would I create: Thanks! Answer Use a list comprehension: A list comprehension lets you apply an expression to each element in a sequence. Here that expression is…

Is there a Python library to list primes?

Is there a library function that can enumerate the prime numbers (in sequence) in Python? I found this question Fastest way to list all primes below N but I’d rather use someone else’s reliable library than roll my own. I’d be happy to do import math; for n in math.primes: Answer Since askin…

Get list of all routes defined in the Flask app

I have a complex Flask-based web app. There are lots of separate files with view functions. Their URLs are defined with the @app.route(‘/…’) decorator. Is there a way to get a list of all the routes that have been declared throughout my app? Perhaps there is some method I can call on the app…

pandas pivot dataframe to 3d data

There seem to be a lot of possibilities to pivot flat table data into a 3d array but I’m somehow not finding one that works: Suppose I have some data with columns=[‘name’, ‘type’, ‘date’, ‘value’]. When I try to pivot via I get Am I reading docs from dev p…

How to define an empty generator function?

A generator function can be defined by putting the yield keyword in the function’s body: How to define an empty generator function? The following code doesn’t work, since Python cannot know that it is supposed to be a generator function instead of a normal function: I could do something like this: But that wo…

Principal Component Analysis (PCA) in Python

I have a (26424 x 144) array and I want to perform PCA over it using Python. However, there is no particular place on the web that explains about how to achieve this task (There are some sites which just do PCA according to their own – there is no generalized way of doing so that I can find). Anybody

Pretty print 2D list?

Is there a simple, built-in way to print a 2D Python list as a 2D matrix? So this: would become something like I found the pprint module, but it doesn’t seem to do what I want. Answer To make things interesting, let’s try with a bigger matrix: Output: UPD: for multiline cells, something like this …

regular expression match starting clause with end

I want to be able to capture the value of an HTML attribute with a python regexp. currently I use My problem is that I want the regular expression to “remember” whether the attribute started with a single or a double quote. I found the bug in my current approach with the following attribute my reg…

Get time of last commit for Git repository files via Python?

I have a Git repository with several thousand files, and would like to get the date and time of the last commit for each individual file. Can this be done using Python (e.g., by using something like os.path.getmtime(path))? Answer An interesting question. Below is a quick and dirty implementation. I’ve …