I use matplotib’s Axes API to plot some figures. One of the lines I plot represents the theoretical expected line. It has no meaning outside of the original y and x limits. What I want, is for matlplotlib to ignore it when autoscaling the limits. What I used to do, is to check what are the current limit…
Tag: python
Add single element to array in numpy
I have a numpy array containing: I want to create an array containing: That is, I want to add the first element on to the end of the array. I have tried the obvious: But I get an error saying ValueError: arrays must have same number of dimensions I don’t understand this – the arrays are both just …
Print a list in reverse order with range()?
How can you produce the following list with range() in Python? Answer use reversed() function: It’s much more meaningful. Update: If you want it to be a list (as btk pointed out): Update: If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step) F…
List from string of digits
I’m getting a string from input() which consists of digits separated by spaces (1 5 6 3). First I’m trying to check that the input only consists of digits with the isdigit() function but because of the spaces I can’t get it to work. Then I convert the string to a list using the split() funct…
What does a . in an import statement in Python mean?
I’m looking over the code for Python’s multiprocessing module, and it contains this line: instead of the subtle difference being the period before _multiprocessing. What does that mean? Why the period? Answer That’s the syntax for explicit relative imports. It means import from the current p…
Automatically call common initialization code without creating __init__.py file
I have two directories in my project: “src” contains my polished code, and “scripts” contains one-off Python scripts. I would like all the scripts to have “../src” added to their sys.path, so that they can access the modules under the “src” tree. One way to do t…
How to replace values at specific indexes of a python list?
If I have a list: And then declare two other lists: How can I take to_modify’s elements as index to indexes, then set corresponding elements in to_modify to replacements, i.e. after running, indexes should be [0,0,3,0,1,0]. Apparently, I can do this through a for loop: But is there other way to do this?…
How to merge dictionaries of dictionaries?
I need to merge multiple dictionaries, here’s what I have for instance: With A B C and D being leaves of the tree, like {“info1″:”value”, “info2″:”value2”} There is an unknown level(depth) of dictionaries, it could be {2:{“c”:{“z”:{…
background function in Python
I’ve got a Python script that sometimes displays images to the user. The images can, at times, be quite large, and they are reused often. Displaying them is not critical, but displaying the message associated with them is. I’ve got a function that downloads the image needed and saves it locally. R…
Is there a Java equivalent of Python’s ‘enumerate’ function?
In Python, the enumerate function allows you to iterate over a sequence of (index, value) pairs. For example: Is there any way of doing this in Java? Answer For collections that implement the List interface, you can call the listIterator() method to get a ListIterator. The iterator has (amongst others) two me…