1 | initial version |
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)
2 | No.2 Revision |
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.
3 | No.3 Revision |
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.
4 | No.4 Revision |
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).