On 21/03/2016 18:26, Maurice wrote:
Hello, hope everything is okay. I think someone might have dealt with a similar
issue I'm having.
Basically I wanna do the following:
I have a list such [6,19,19,21,21,21] (FYI this is the item of a certain key in
the dictionary)
And I need to convert it to a list of 32 elements (meaning days of the month
however first element ie index 0 or day zero has no meaning - keeping like that
for simplicity's sake).
Therefore the resulting list should be:
[0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]
So the list index should tell how many occurrences of a certain day in the
original list.
My attempt:
weirdList = [[0]*32]*len(dict_days) #list's length should be how many keys in
the dict.
counter = 0
k = 0
for key in dict_days.keys():
for i in range(1,32):
if i in dict_days[key]:
counter = dict_days[key].count(i)
weirdList[k][i] = counter
dict_days[key] = weirdList[k]
k+=1
However it does not work. weirdList seems to be always the same?
Thanks in advance.
>>> from collections import Counter
>>> counts = Counter([6,19,19,21,21,21])
>>> counts
Counter({21: 3, 19: 2, 6: 1})
>>> weird_list = [0]*32
>>> weird_list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0]
>>> for index, count in counts.items():
... weird_list[index] = count
...
>>> weird_list
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0]
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
--
https://mail.python.org/mailman/listinfo/python-list