Ask Your Question

Revision history [back]

If you do not know in advance which numbers will belong to your list results, my favorite is the following:

sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in results:                  
....:     d[i] += 1
sage: points((i,d[i]) for i in d)

If you do not know in advance which numbers will belong to your list results, my favorite is the following:

sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in results:                  
....:     d[i] += 1
sage: points((i,d[i]) for i in d)

The problem with using the count method of lists is that, for each value in the list, the whole list is read, so it takes a linear amount of time. Hence, if all values are different, the global time will be quadratic.

If you do not know in advance which numbers will belong to your list results, my favorite is the following:

sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in results:                  
results:
....:     d[i] += 1
sage: points((i,d[i]) for i in d)

The problem with using the count method of lists is that, for each value in the list, the whole list is read, so it takes a linear amount of time. Hence, if all values are different, the global time will be quadratic.

If you do not know in advance which numbers will belong to your list results, my favorite is the following:

sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in results:
....:     d[i] += 1
sage: points((i,d[i]) for i in d)

The problem with using the count method of lists is that, for each value in the list, the whole list is read, so it takes a linear amount of time. Hence, if all values are different, the global time will be quadratic.

With dictionary (as we did), the global time will be linear (in average, assuming the hash of your object does not create too much collisions, see https://wiki.python.org/moin/TimeComplexity#dict).