# Revision history [back]

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


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


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


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


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


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


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


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