![]() |
List showing running total of instances - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: List showing running total of instances (/thread-1635.html) Pages:
1
2
|
List showing running total of instances - Pie_Fun - Jan-17-2017 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 RE: List showing running total of instances - micseydel - Jan-17-2017 What have you tried? RE: List showing running total of instances - Pie_Fun - Jan-18-2017 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 RE: List showing running total of instances - sparkz_alot - Jan-18-2017 (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. RE: List showing running total of instances - Pie_Fun - Jan-18-2017 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() RE: List showing running total of instances - wavic - Jan-18-2017 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() RE: List showing running total of instances - Mekire - Jan-18-2017 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)
RE: List showing running total of instances - wavic - Jan-18-2017 I was thinking that this is homework ![]() Very generously ![]() RE: List showing running total of instances - micseydel - Jan-18-2017 There was some arguably off-topic discussion which I've split out into https://python-forum.io/Thread-split-Time-Complexity-of-Counting RE: Dictionary showing running total of instances - Pie_Fun - Jan-20-2017 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 ![]() cum_countThanks of course. I've just tweaked it for my needs. |