Circular pairs from array?

I can't believe this is nowhere to be found, but: I want all consecutive pairs from an array, including the last element with the first one. I tried: [(a, b) for a, b in zip(list, list[1:])] What is the most pythonic and efficient way to do it? ...
more »

2017-02-13 23:02 (4) Answers

Adding matrices with different dimensions

In numpy and tensorflow it's possible to add matrices (or tensors) of different dimensionality if the shape of smaller matrix is a suffix of bigger matrix. This is an example: x = np.ndarray(shape=(10, 7, 5), dtype = float) y = np.ndarray(shape=(7, ...
more »

2017-02-13 22:02 (2) Answers

Why django urls end with a slash?

Django official documentation and other tutorials on the web always use a trailing slash at the end of url. ex: url(r'^accounts/login/', views.login) # login view in turn calls login.html # instead of url(r'^accounts/login', view.login) Since a...
more »

2017-02-13 20:02 (3) Answers

python: can statement be inside expression?

I'm trying to implement a lazy property. Say, an object is initialize by object._x = None, and then I want to write something @property def x(self): return self._x or (init_x_value(); x) so that the initializer is called only the first time th...
more »

2017-02-13 13:02 (3) Answers

python opencv - blob detection or circle detection

I am having problems detecting circle areas. I tried it with the HoughCircles function from opencv. However even though the images are pretty similar the parameters for the funtion have to be different in order to detect the cirles. Another approac...
more »

2017-02-13 13:02 (2) Answers

Order of operations in a dictionary comprehension

I came across the following interesting construct: assuming you have a list of lists as follows: my_list = [['captain1', 'foo1', 'bar1', 'foobar1'], ['captain2', 'foo2', 'bar2', 'foobar2'], ...] and you want to create a dict out of them with the ...
more »

2017-02-13 11:02 (4) Answers

Documentation of usage examples for Python library

I am developing a new library in Python. As I already know that my audience includes engineers with very little Python experience, I would like to add examples to this library in order to make it easier for them to get started. I've already discuss...
more »

2017-02-12 10:02 (0) Answers

When to use return (yield something)?

For a long time I didn't know you can't put return in front of a yield statement. But actually you can: def gen(): return (yield 42) which is similar to def gen(): yield 42 return And the only usage I can think of is to attach sent ...
more »

2017-02-12 06:02 (0) Answers

Sorting Pandas DataFrames

I have a Pandas Dataframe with car data. I want to find the best selling two Models for each Maker, and then rank the Makers, descending. Maker Model No Sold(,000s) Ford Kuga 35 Ford Focus 47 Ford Ka 31 Ford Fiesta ...
more »

2017-02-12 01:02 (3) Answers

python pyxb UnrecognizedDOMRootNodeError(node)

I am trying to create a pyxb module from the LabVIEW schema. Using the module I am getting an UnrecognizedDOMRootNodeError: Traceback (most recent call last): File "X:\Projects\LV-PY\Python\LV-PY\pyXB\testXML.py", line 14, in <module> f...
more »

2017-02-11 20:02 (0) Answers

Django: Avoid showing error details in production

I am trying to make sure that my site is properly protected from showing the details of the error in production. I've been struggling with this for a while, as at beginning I understood that in order to avoid Django from showing the error (module, l...
more »

2017-02-11 19:02 (1) Answers

Advanced input in python

I want to receive some information from a user in a next way: My score is of 10 - is already printed Between 'is' and 'of' there is an empty place for user's input so he doesn't enter his information at the end( if using simple input() ) but in ...
more »

2017-02-11 14:02 (2) Answers

Python: pandas apply vs. map

I am struggling to understand how df.apply()exactly works. My problem is as follows: I have a dataframe df. Now I want to search in several columns for certain strings. If the string is found in any of the columns I want to add for each row where t...
more »

2017-02-11 12:02 (3) Answers