Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

I was having the same problem just now.

After adding the proper folder (**C:\Python33\Scripts**) to the path, I still could not get **pip** to run. All it took was running **pip.exe install -package-** instead of **pip install -package-**.
4 years ago
I usually use a construct similar to this:

***
* Determine if a variable is iterable. i.e. can be used to loop over.
*
* @return bool
*/
function is_iterable($var)
{
return $var !== null
&& (is_array($var)
|| $var instanceof Traversable
|| $var instanceof Iterator
|| $var instanceof IteratorAggregate
);
}

$values = get_values();

if (is_iterable($values))
{
foreach ($values as $value)
{
// do stuff...
}
} ***
Note that this particular version is not tested, it's typed directly into SO from memory.
4 years ago
Well, it is actually possible to do a "sort by dictionary values". Recently I had to do that in a Code Golf (Stack Overflow question Code golf: Word frequency chart). Abridged, the problem was of the kind: given a text, count how often each word is encountered and display a list of the top words, sorted by decreasing frequency.

If you construct a dictionary with the words as keys and the number of occurrences of each word as value, simplified here as:

from collections import defaultdict
d = defaultdict(int)
for w in text.split():
d[w] += 1
then you can get a list of the words, ordered by frequency of use with sorted(d, key=d.get) - the sort iterates over the dictionary keys, using the number of word occurrences as a sort key .

for w in sorted(d, key=d.get, reverse=True):
print(w, d[w])
I am writing this detailed explanation to illustrate what people often mean by "I can easily sort a dictionary by key, but how do I sort by value" - and I think the original post was trying to address such an issue. And the solution is to do sort of list of the keys, based on the values, as shown above.
4 years ago