ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 24 Jul 2018 10:55:17 +0200Finding the frequency of each number in a stringhttps://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/ Hi, I have a string of numbers like s=[(11,23),(33,47),(98,20),...,(34,65)] produced by a code in Jupyter. The biggest number is 100. How can I order the numbers based on their frequency of repetition in s? For example for a simple case like s1=[(1,4),(2,4),(4,1)] the result is
Number 4 frequency 3
Number 1 frequency 2
number 2 frequency 1Sun, 22 Jul 2018 11:59:41 +0200https://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/Answer by tmonteil for <p>Hi, I have a string of numbers like s=[(11,23),(33,47),(98,20),...,(34,65)] produced by a code in Jupyter. The biggest number is 100. How can I order the numbers based on their frequency of repetition in s? For example for a simple case like s1=[(1,4),(2,4),(4,1)] the result is</p>
<p>Number 4 frequency 3
Number 1 frequency 2
number 2 frequency 1</p>
https://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/?answer=43097#post-id-43097First, 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 0Sun, 22 Jul 2018 12:22:57 +0200https://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/?answer=43097#post-id-43097Comment by Sébastien for <p>First, your data is a list of tuples, so you have to flatten it to make it a simple list:</p>
<pre><code>sage: s=[(11,23),(33,47),(98,20),(34,65)]
sage: flatten(s)
[11, 23, 33, 47, 98, 20, 34, 65]
</code></pre>
<p>Then, my favorite way to count occurrences of unknown objects is <code>defaultdict</code>:</p>
<pre><code>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})
</code></pre>
<p>Then you can ask for th frequency of the numbers that appeared:</p>
<pre><code>sage: d[4]
3
sage: d[1]
2
sage: d[2]
1
</code></pre>
<p>But also numbers that did no appear:</p>
<pre><code>sage: d[12]
0
</code></pre>
<p>See also this answer : <a href="https://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/">https://ask.sagemath.org/question/396...</a></p>
<p>Then you can sort the keys of the dictonary according to theirs value:</p>
<pre><code>sage: sorted(d, key=d.get, reverse=True)
[4, 1, 2, 12]
</code></pre>
<p>(note that when we called <code>d[12]</code> it created a "real" entry for it)</p>
<p>Then you can do something like:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/?comment=43099#post-id-43099Instead of using `defaultdict(int)`, one may use `Counter` also from `collections`:
>>> from collections import Counter
>>> c = Counter()
>>> c['a'] += 1
>>> c[2] += 1
>>> c[2] += 1
>>> c
Counter({2: 2, 'a': 1})
or even better:
>>> Counter([2,2,'a'])
Counter({2: 2, 'a': 1})Sun, 22 Jul 2018 13:47:36 +0200https://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/?comment=43099#post-id-43099Comment by ASH for <p>First, your data is a list of tuples, so you have to flatten it to make it a simple list:</p>
<pre><code>sage: s=[(11,23),(33,47),(98,20),(34,65)]
sage: flatten(s)
[11, 23, 33, 47, 98, 20, 34, 65]
</code></pre>
<p>Then, my favorite way to count occurrences of unknown objects is <code>defaultdict</code>:</p>
<pre><code>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})
</code></pre>
<p>Then you can ask for th frequency of the numbers that appeared:</p>
<pre><code>sage: d[4]
3
sage: d[1]
2
sage: d[2]
1
</code></pre>
<p>But also numbers that did no appear:</p>
<pre><code>sage: d[12]
0
</code></pre>
<p>See also this answer : <a href="https://ask.sagemath.org/question/39677/plotting-a-frequency-chart-from-a-set-of-values/">https://ask.sagemath.org/question/396...</a></p>
<p>Then you can sort the keys of the dictonary according to theirs value:</p>
<pre><code>sage: sorted(d, key=d.get, reverse=True)
[4, 1, 2, 12]
</code></pre>
<p>(note that when we called <code>d[12]</code> it created a "real" entry for it)</p>
<p>Then you can do something like:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/?comment=43128#post-id-43128Thanks a lot.Tue, 24 Jul 2018 10:55:17 +0200https://ask.sagemath.org/question/43096/finding-the-frequency-of-each-number-in-a-string/?comment=43128#post-id-43128