Given a sorted list of numbers, I need to find the smallest number that is greater than a given number. Consider this list: Say the specified number is 320. Then, my method should return 353 as 353 is the smallest number greater than 320. I am trying to use a slightly modified form of binary search; however o…
Tag: python
How do I Pass a List of Series to a Pandas DataFrame?
I realize Dataframe takes a map of {‘series_name’:Series(data, index)}. However, it automatically sorts that map even if the map is an OrderedDict(). Is there a simple way to pass a list of Series(data, index, name=name) such that the order is preserved and the column names are the series.name? Is…
How do I concatenate text files in Python?
I have a list of 20 file names, like [‘file1.txt’, ‘file2.txt’, …]. I want to write a Python script to concatenate these files into a new file. I could open each file by f = open(…), read line by line by calling f.readline(), and write each line into that new file. It doesn…
How do I find the difference between two values without knowing which is larger?
I was wondering if there was a function built into Python that can determine the distance between two rational numbers but without me telling it which number is larger. e.g. Obviously I could write a simple definition to calculate which is larger and then just do a simple subtraction: but I’d rather not…
Does Python optimize tail recursion?
I have the following piece of code which fails with the following error: RuntimeError: maximum recursion depth exceeded I attempted to rewrite this to allow for tail recursion optimization (TCO). I believe that this code should have been successful if a TCO had taken place. Should I conclude that Python does …
Do Python regular expressions have an equivalent to Ruby’s atomic grouping?
Ruby’s regular expressions have a feature called atomic grouping (?>regexp), described here, is there any equivalent in Python’s re module? Answer Python does not directly support this feature, but you can emulate it by using a zero-width lookahead assert ((?=RE)), which matches from the curren…
How can I find the dimensions of a matrix in Python?
How can I find the dimensions of a matrix in Python. Len(A) returns only one variable. Edit: Is (I assume) generating a matrix of integers (less likely strings). I need to find the size of that matrix, so I can run some tests without having to iterate through all of the elements. As far as the data type goes,…
Python equivalent of npm or rubygems?
I’ve been looking around for a package manager that can be used with python. I want to list project dependencies in a file. For example ruby uses Gemfile where you can use bundle install. How can I achieve this in Python? Answer The pip tool is becoming the standard in equivalent of Ruby’s gems. L…
Python: Catching specific exception
I want to catch a specific ValueError, not just any ValueError. I tried something like this: But it raises a SyntaxError: can’t assign to literal. Then I tried: But it raises the exception, even if it is the one I want to avoid. Answer in except ValueError,e, e is an instance of the exception, not a str…
Recursive DotDict
I have a utility class that makes Python dictionaries behave somewhat like JavaScript objects as far as getting and setting attributes. I would like to make it so it also converts nested dictionaries into DotDict() instances. I was hoping to be able to do something like this with __init__ or __new__, but I ha…