Posts: 4
Threads: 1
Joined: Jan 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
Posts: 2,342
Threads: 62
Joined: Sep 2016
Posts: 4
Threads: 1
Joined: Jan 2017
Jan-18-2017, 11:00 AM
(This post was last modified: Jan-18-2017, 11:00 AM by Pie_Fun.)
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
Posts: 1,298
Threads: 38
Joined: Sep 2016
(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
Posts: 4
Threads: 1
Joined: Jan 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()
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-18-2017, 04:10 PM
(This post was last modified: Jan-18-2017, 04:10 PM by wavic.)
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()
Posts: 591
Threads: 26
Joined: Sep 2016
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]
Posts: 2,953
Threads: 48
Joined: Sep 2016
I was thinking that this is homework
Very generously
Posts: 2,342
Threads: 62
Joined: Sep 2016
There was some arguably off-topic discussion which I've split out into https://python-forum.io/Thread-split-Tim...f-Counting
Posts: 4
Threads: 1
Joined: Jan 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 .(got something on your mind? )
cum_count Thanks of course. I've just tweaked it for my needs.
|