Skip to content

Tag: python

How does python numpy.where() work?

I am playing with numpy and digging through documentation and I have come across some magic. Namely I am talking about numpy.where(): How do they achieve internally that you are able to pass something like x > 5 into a method? I guess it has something to do with __gt__ but I am looking for a detailed expla…

Project Euler Problem 17 Python

Please let me know how to bug fix this code . I tried and correct a lot of things , but I am 10 extra to the solution ! If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters

How to urlencode a querystring in Python?

I am trying to urlencode this string before I submit. Answer You need to pass your parameters into urlencode() as either a mapping (dict), or a sequence of 2-tuples, like: Python 3 or above Use urllib.parse.urlencode: Note that this does not do url encoding in the commonly used sense (look at the output). For…

Select NULL Values in SQLAlchemy

Here’s my (PostgreSQL) table — I want to select all people that are not known to be married, i.e., including those with NULL marriage_status. This does not work — Of course this does — The problem is that I’m accessing it from SQLAlchemy with — which gets translated to &#82…

Python how to reduce multiple lists?

I am able to use map and sum to achieve this functionality, but how to use reduce? There are 2 lists: a, b, they have same number of values. I want to calculate The working version I wrote using map is How to use reduce then? I wrote: I got the error “TypeError: ‘float’ object is unsubscript…

How to copy a dict and modify it in one line of code

Very often I need to create dicts that differ one from another by an item or two. Here is what I usually do: The fact that there is a point in the program at which setup2 is an identical copy of setup1 makes me nervous, as I’m afraid that at some point of the program life the two lines might

Immutable numpy array?

Is there a simple way to create an immutable NumPy array? If one has to derive a class from ndarray to do this, what’s the minimum set of methods that one has to override to achieve immutability? Answer You can make a numpy array unwriteable: Also see the discussion in this thread: http://mail.scipy.org…