Python Forum
List showing running total of instances
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List showing running total of instances
#1
Hi Folks,

to try and explain what I'm trying to do
Say I have a list of numbers
48,52,55,48,52,55,60,62,48
what I want is a corresponding list with the number of instances that a number has appeared to that point in the list.
so it would be
1,1,1,2,2,2,1,1,3

Thanks for any feedback
Reply
#2
What have you tried?
Reply
#3
ok i've got it sorted
if its of benefit to anybody else

from collections import Counter
AList = [1,2,1,2,3,2,1]
BList = []
CList = []
c = collections.Counter()
myDict = {}
count = 1
for x in AList:
BList.append(x)
for y in BList:
CList.append(y)
c.update(CList)
BList = []
myDict[count] = {'notecount':dict©}
count = count + 1
c = collections.Counter()

ps can't easily see how to to put the code into a format for the forum - so spacing is off and dict© is mangled as a copyright symbol
Reply
#4
(Jan-18-2017, 11:00 AM)Pie_Fun Wrote: ps can't easily see how to to put the code into a format for the forum - so spacing is off and dict© is mangled as a copyright symbol

Which makes it pretty much useless to anyone wishing to read or use your solution.  Check the Help document at the top of the page.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
from collections import Counter           
aList = [1,2,1,2,3,2,1]
bList = []
cList = []
c = collections.Counter()
myDict = {}
count = 1
for x in aList:  
   bList.append(x)
   for y in bList:
       cList.append(y)
   c.update(finalList)
   bList = []
   myDict[count] = {'notecount':dict(c)}
   count = count + 1
   c = collections.Counter()
Reply
#6
You need to count an element in the incrementing slices of the first list and append the result for an element to the new list. You don't need 2 additional list neather a dict.

In [19]: for s, i in enumerate(l,1):
    ...:     print(l[:s])
    ...:     
[48]
[48, 52]
[48, 52, 55]
[48, 52, 55, 48]
[48, 52, 55, 48, 52]
[48, 52, 55, 48, 52, 55]
[48, 52, 55, 48, 52, 55, 60]
[48, 52, 55, 48, 52, 55, 60, 62]
[48, 52, 55, 48, 52, 55, 60, 62, 48]
Also, you will get an error message at the very beginning in your code.
In [1]: from collections import Counter

In [2]: c = collections.Counter()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-927bc8fd8136> in <module>()
----> 1 c = collections.Counter()

NameError: name 'collections' is not defined

In [3]: c = Counter()

In [4]: c
Out[4]: Counter()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
I don't think collections.Counter offers much here as you need to do it every step.

One loop will do:
sequence = 48,52,55,48,52,55,60,62,48

result = []
cum_count = {}
for num in sequence:
    cum_count[num] = cum_count.get(num, 0) + 1
    result.append(cum_count[num])

print(result)
Output:
[1, 1, 1, 2, 2, 2, 1, 1, 3]
Reply
#8
I was thinking that this is homework  Think

Very generously  Cool
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
There was some arguably off-topic discussion which I've split out into https://python-forum.io/Thread-split-Tim...f-Counting
Reply
#10
Hi thanks for the feedback and all the responses

Ok First thing for any future viewers

i screwed up the copy and paste -
should have been
from collections import Counter
AList = [1,2,1,2,3,2,1]
BList = []
CList = []
c = collections.Counter()
myDict = {}
count = 1
for x in AList:
   BList.append(x)
   for y in BList:
       CList.append(y)
       c.update(CList)
       BList = []
   myDict[count] = {'notecount':dict(c)}
   count = count + 1
   c = collections.Counter()
re:  >> Also, you will get an error message at the very beginning in your code.
Funny, I'm not getting that error (I'm running from a python tab in maya dont know if that would be any reason)

thanks for the various suggestions - some useful things to know. wavic - Just to clarify I should have titled my original post 'dictionary showing..." rather than 'list showing..'.  As I need to be able access the key:value for the coding in maya. What I've coded works for me but over engineered so the winner is Mekire  Wink  .(got something on your mind? )
cum_count
Thanks of course. I've just tweaked it for my needs.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python List with Total and Average mcnhscc39 3 3,088 Nov-28-2018, 09:02 AM
Last Post: DeaD_EyE
  TypeError: '<=' not supported between instances of 'list' and 'int' Leonzxd 4 62,969 May-18-2018, 09:49 AM
Last Post: Leonzxd
  applying total in a loop to a list JustinxFFx 1 2,187 Feb-11-2018, 03:14 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020