0

I am writing a piece of code that iterates through each record and prints a statistic called intervals.

for record in records:

    from collections import Counter
    count = Counter(intervals)

    for interval, frequency in count.iteritems():
        print interval
        print frequency

The output looks like this:

Record 1
199
7
200
30

Record 2
199
6
200
30

In this example, in Record 1, there are 7 instances of the interval length 199 and 30 instances of the interval length 200. In Record 2, there are 6 instances of the interval length 199 and 31 instances of the interval length 200. I would like to see the overall stat summary of both records like this but cannot figure out how to get these results:

All Records

199
13

200
61

Where in both records, there are 13 instances total of interval length 199 (7+6) and 61 instances total of interval length 200 (30+31). I am having trouble getting the overall statistic summary of my records as shown above.

martineau
  • 119,623
  • 25
  • 170
  • 301
nikki_c
  • 117
  • 1
  • 3
  • 7
  • 2
    Could you please clarify your question by editing your post? – liam Aug 07 '17 at 16:24
  • 1
    I'm sorry, but what exactly is your question? Also, please fix your indentation. – juanpa.arrivillaga Aug 07 '17 at 16:25
  • 1
    What do you want to know exactly ? Explain Clearly ... – Md. Rezwanul Haque Aug 07 '17 at 16:26
  • 1
    Please indent your code properly. You can just add a variable that you add 1 to on every iteration. I would amend your code with a fuller explaination but I'm uncertain which peices of code belong in which for loop {ahhh the beauty of braces} – DrBwts Aug 07 '17 at 16:32
  • What's stored in `intervals` and in what format? – martineau Aug 07 '17 at 16:39
  • 2
    I think you want a `total = Counter()` outside the loop, then each time you do `count = Counter(intervals)` do a `total.update(count)`... then print `total` outside your loop? That'll give you cumulative totals along the way and a total total at the end... (but I'm not entirely sure...) – Jon Clements Aug 07 '17 at 16:40
  • intervals = [199,200,200,199,199, ......] It's an array of numbers. – nikki_c Aug 07 '17 at 16:42
  • 1
    @nikki_c or maybe `counts = Counter(); for record in records: counts.update(record)`... then do whatever you want with `counts`? – Jon Clements Aug 07 '17 at 16:45
  • 1
    @JonClements with `Counter` objects, you could just do `total += count` – juanpa.arrivillaga Aug 07 '17 at 17:09

1 Answers1

1

You need variable outside for loop that store the frequency counting Following example may help you.

from collections import Counter


records = [[199,200,200], [200,199,200]]
freq_dist = Counter()                        # Variable store frequency distribution

for record in records:
    counts = Counter(record)
    freq_dist.update(counts)

for freq, value in freq_dist.items():
   print(freq, value)

Output:

200 4
199 2

Reference collections.Counter

pylang
  • 40,867
  • 14
  • 129
  • 121
Kaushal
  • 666
  • 6
  • 15