If list index exists, do X
Could it be more useful for you to use the length of the list len(n) to inform your decision rather than checking n[i] for each possible length?
Could it be more useful for you to use the length of the list len(n) to inform your decision rather than checking n[i] for each possible length?
A nested dict is a dictionary within a dictionary. A very simple thing. >>> d = {} >>> d[‘dict1’] = {} >>> d[‘dict1’][‘innerkey’] = ‘value’ >>> d[‘dict1’][‘innerkey2’] = ‘value2’ >>> d {‘dict1’: {‘innerkey’: ‘value’, ‘innerkey2’: ‘value2’}} You can also use a defaultdict from the collections package to facilitate creating nested dictionaries. >>> import collections >>> … Read more
The changelog is sloppily worded. from __future__ import absolute_import does not care about whether something is part of the standard library, and import string will not always give you the standard-library module with absolute imports on. from __future__ import absolute_import means that if you import string, Python will always look for a top-level string module, … Read more
This is a pure guess, and I haven’t figured out an easy way to check whether it is right, but I have a theory for you. I tried your code and get the same of results, without_else() is repeatedly slightly slower than with_else(): >>> T(lambda : without_else()).repeat() [0.42015745017874906, 0.3188967452567226, 0.31984281521812363] >>> T(lambda : with_else()).repeat() [0.36009842032996175, … Read more
The problem is that for json.load you should pass a file like object with a read function defined. So either you use json.load(response) or json.loads(response.read()).
I was getting the same error and was able to solve it by updating my numpy installation to 1.8.0: pip install -U numpy
pip install –ignore-installed six Would do the trick. Source: github.com/pypa/pip/issues/3165
The approach you should take is to install pip for Python 3.2. You do this in the following way: $ curl -O https://bootstrap.pypa.io/get-pip.py $ sudo python3.2 get-pip.py Then, you can install things for Python 3.2 with pip-3.2, and install things for Python 2-7 with pip-2.7. The pip command will end up pointing to one of … Read more
Yes, if bar is not None is more explicit, and thus better, assuming it is indeed what you want. That’s not always the case, there are subtle differences: if not bar: will execute if bar is any kind of zero or empty container, or False. Many people do use not bar where they really do … Read more
If I had to guess, you did this: import datetime at the top of your code. This means that you have to do this: datetime.datetime.strptime(date, “%Y-%m-%d”) to access the strptime method. Or, you could change the import statement to this: from datetime import datetime and access it as you are. The people who made the … Read more