I have a large list like: I want to build a multi-level dict like: I know that if I use recursive defaultdict I can write table[A][B1][C1]=1, table[A][B2]=2, but this works only if I hardcode those insert statement. While parsing the list, I don’t how many []’s I need beforehand to call table[key1…
Tag: python
Why doesn’t Pydev find Django?
I have Django installed. I also have django-admin.py in my python-2.7 folder. I can import django using my Python shell (DreamPie). But I can’t create a new Django project in Pydev. When trying to create a new Pydev Django Project (choosing python2.7 as the interpreter) I receive the following error mes…
Histogram Matplotlib
So I have a little problem. I have a data set in scipy that is already in the histogram format, so I have the center of the bins and the number of events per bin. How can I now plot is as a histogram. I tried just doing but it didn’t like that. Any recommendations? Answer The object-oriented interface i…
How to round each item in a list of floats to 2 decimal places?
I have a list which consists of float values but they’re too detailed to proceed. I know we can shorten them by using the (“%.f” % variable) operator, like: My question is how can I turn a list of values into their rounded equivalents without using an iterator. I’ve tried something, bu…
How to merge a transparent png image with another image using PIL
I have a transparent png image foo.png and I’ve opened another image with: Now what I need is to merge foo.png with foo2.png. (foo.png contains some text and I want to print that text on foo2.png) Answer First parameter to .paste() is the image to paste. Second are coordinates, and the secret sauce is t…
How to delete an AMI using boto?
(cross posted to boto-users) Given an image ID, how can I delete it using boto? Answer You use the deregister() API. There are a few ways of getting the image id (i.e. you can list all images and search their properties, etc) Here is a code fragment which will delete one of your existing AMIs (assuming itR…
Python zipfile module: difference between zipfile.ZIP_DEFLATED and zipfile.ZIP_STORED
I have difficulty understanding the difference between zipfile.ZIP_DEFLATED and zipfile.ZIP_STORED compression modes of the zipfile module. Answer ZIP_DEFLATED correspond to an archive member (a file inside the archive) which is compressed (or deflated). ZIP_STORED correspond to an archive member which is sim…
How to get MD5 sum of a string using python?
In the Flickr API docs, you need to find the MD5 sum of a string to generate the [api_sig] value. How does one go about generating an MD5 sum from a string? Flickr’s example: string: 000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite MD5 sum: a02506b31c1cd46c2e0b6380fb94eb3d Answer For Py…
Rank items in an array using Python/NumPy, without sorting array twice
I have an array of numbers and I’d like to create another array that represents the rank of each item in the first array. I’m using Python and NumPy. For example: Here’s the best method I’ve come up with: Are there any better/faster methods that avoid sorting the array twice? Answer Us…
Plot smooth line with PyPlot
I’ve got the following simple script that plots a graph: As it is now, the line goes straight from point to point which looks ok, but could be better in my opinion. What I want is to smooth the line between the points. In Gnuplot I would have plotted with smooth cplines. Is there an easy way to do this