Skip to content

Tag: python

Python – Flatten a dict of lists into unique values?

I have a dict of lists in python: I want to turn it into a list of the unique values I wrote a manual solution, but it’s a manual solution. I know there are more concise and more elegant way to do this with list comprehensions, map/reduce , itertools , etc. anyone have a clue ? Answer Double set compreh…

Check if variable is defined with Numpy array?

Sometimes I have a situation where I want to test whether a variable is 0 or None or not. In pure Python, this is simply but when foo is possibly a Numpy object (such as numpy.ndarray), this does not work anymore and I get the error: and in this case I want a.any(), however this fails on non-iterable objects.…

Is there a short contains function for lists?

Given a list xs and a value item, how can I check whether xs contains item (i.e., if any of the elements of xs is equal to item)? Is there something like xs.contains(item)? For performance considerations, see Fastest way to check if a value exists in a list. Answer Use: Also, inverse operation: It works fine …

Nohup is not writing log to output file

I am using the following command to run a python script in the background: But it appears that nohup is not writing anything to the log file. cmd.log is created but is always empty. In the python script, I am using sys.stdout.write instead of print to print to standard output. Am I doing anything wrong? Answe…

How to check if the object has property in view in Django?

I am trying to get the documents property in a general function, but a few models may not have the documents attribute. Is there any way to first check if a model has the documents property, and then conditionally run code? Answer You can use hasattr() to check to see if model has the documents property. Howe…

Why wasn’t PyPy included in standard Python?

I was looking at PyPy and I was just wondering why it hasn’t been adopted into the mainline Python distributions. Wouldn’t things like JIT compilation and lower memory footprint greatly improve the speeds of all Python code? In short, what are the main drawbacks of PyPy that cause it to remain a s…