1 | initial version |
First, your data is a list of tuple, so you have to flatten it to make it a list:
sage: s=[(11,23),(33,47),(98,20),(34,65)]
sage: flatten(s)
[11, 23, 33, 47, 98, 20, 34, 65]
Then, my favorite way to count occurrences of unknown objects is defaultdict
:
sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in flatten(s1):
....: d[i] += 1
sage: d
defaultdict(<type 'int'>, {1: 2, 2: 1, 4: 3})
Then you can ask for th frequency of the numbers that appeared:
sage: d[4]
3
sage: d[2]
1
But also numbers that did no appear:
sage: d[12]
0
2 | No.2 Revision |
First, your data is a list of tuple, so you have to flatten it to make it a list:
sage: s=[(11,23),(33,47),(98,20),(34,65)]
sage: flatten(s)
[11, 23, 33, 47, 98, 20, 34, 65]
Then, my favorite way to count occurrences of unknown objects is defaultdict
:
sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in flatten(s1):
....: d[i] += 1
sage: d
defaultdict(<type 'int'>, {1: 2, 2: 1, 4: 3})
Then you can ask for th frequency of the numbers that appeared:
sage: d[4]
3
sage: d[1]
2
sage: d[2]
1
But also numbers that did no appear:
sage: d[12]
0
See also this answer : https://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/
3 | No.3 Revision |
First, your data is a list of tuple, so you have to flatten it to make it a list:
sage: s=[(11,23),(33,47),(98,20),(34,65)]
sage: flatten(s)
[11, 23, 33, 47, 98, 20, 34, 65]
Then, my favorite way to count occurrences of unknown objects is defaultdict
:
sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in flatten(s1):
....: d[i] += 1
sage: d
defaultdict(<type 'int'>, {1: 2, 2: 1, 4: 3})
Then you can ask for th frequency of the numbers that appeared:
sage: d[4]
3
sage: d[1]
2
sage: d[2]
1
But also numbers that did no appear:
sage: d[12]
0
See also this answer : https://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/
Then you can sort the keys of the dictonary according to theirs value:
sage: sorted(d, key=d.get, reverse=True)
[4, 1, 2, 12]
(note that when we called d[12]
it created a "real" entry for it)
Then you can do something like:
sage: for k in sorted(d, key=d.get, reverse=True):
....: print '{} has freqency {}'.format(k, d[k])
4 has freqency 3
1 has freqency 2
2 has freqency 1
12 has freqency 0
4 | No.4 Revision |
First, your data is a list of tuple, tuples, so you have to flatten it to make it a simple list:
sage: s=[(11,23),(33,47),(98,20),(34,65)]
sage: flatten(s)
[11, 23, 33, 47, 98, 20, 34, 65]
Then, my favorite way to count occurrences of unknown objects is defaultdict
:
sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in flatten(s1):
....: d[i] += 1
sage: d
defaultdict(<type 'int'>, {1: 2, 2: 1, 4: 3})
Then you can ask for th frequency of the numbers that appeared:
sage: d[4]
3
sage: d[1]
2
sage: d[2]
1
But also numbers that did no appear:
sage: d[12]
0
See also this answer : https://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/
Then you can sort the keys of the dictonary according to theirs value:
sage: sorted(d, key=d.get, reverse=True)
[4, 1, 2, 12]
(note that when we called d[12]
it created a "real" entry for it)
Then you can do something like:
sage: for k in sorted(d, key=d.get, reverse=True):
....: print '{} has freqency {}'.format(k, d[k])
4 has freqency 3
1 has freqency 2
2 has freqency 1
12 has freqency 0
5 | No.5 Revision |
First, your data is a list of tuples, so you have to flatten it to make it a simple list:
sage: s=[(11,23),(33,47),(98,20),(34,65)]
sage: flatten(s)
[11, 23, 33, 47, 98, 20, 34, 65]
Then, my favorite way to count occurrences of unknown objects is defaultdict
:
sage: from collections import defaultdict
sage: d = defaultdict(int)
sage: for i in flatten(s1):
....: d[i] += 1
sage: d
defaultdict(<type 'int'>, {1: 2, 2: 1, 4: 3})
Then you can ask for th frequency of the numbers that appeared:
sage: d[4]
3
sage: d[1]
2
sage: d[2]
1
But also numbers that did no appear:
sage: d[12]
0
See also this answer : https://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/
Then you can sort the keys of the dictonary according to theirs value:
sage: sorted(d, key=d.get, reverse=True)
[4, 1, 2, 12]
(note that when we called d[12]
it created a "real" entry for it)
Then you can do something like:
sage: for k in sorted(d, key=d.get, reverse=True):
....: print '{} 'Number {} has freqency {}'.format(k, d[k])
Number 4 has freqency 3
Number 1 has freqency 2
Number 2 has freqency 1
Number 12 has freqency 0