简体   繁体   中英

Mapping dictionary key values in a list

Suppose I have list t=[3 ,3, 4, 0, 0, 3, 1, 0, 4, 3, 3, 3, 0] and it's subset as
t_old=[3, 3, 4, 0, 0] now I have to calculate the fequency of items which are not in t_old ( [3,1, 0, 4, 3, 3, 3, 0]) , which I have calculated N={0:2,1:1,3:3,4:1} but after that, I have to map these values in a list Nk of size 5 such that N={0:2,1:1,3:4,4:1} so list will be NK=[2,1,0,4,1] 0->2,1->1,2->0 (since no frequency of 2),3->3,4->1 so NK is[2,1,0,4,1] also the order is important My code is

from collections import Counter, defaultdict
import operator


t=[3 ,3, 4, 0, 0, 3, 1, 0, 4, 3, 3, 3, 0]
t_old=[3 ,3, 4, 0, 0]
cluster=5
nr=[]
k=len(t)-len(t_old)
print("Value of k is\n",k)
z=len(t_old)
while(k):
    nr.append(t[z])
    z+=1
    k-=1

print("Value of z is\n",nr)
nr=Counter(nr)  #Counter that calculates the item that are not in t_old
print("counter is\n",nr)
d1 = dict(sorted(nr.items(), key = lambda x:x[0])) #Sorting the nr according to the key
print("Value of sorted dictonary is\n",d1)

So I want the output in the form of list

NK is[2,1,0,4,1]

how can I get that output pls help thanks in advance

>>> from collections import Counter
>>> t=[3 ,3, 4, 0, 0, 3, 1, 0, 4, 3, 3, 3, 0]
>>> t_old=[3, 3, 4, 0, 0]
>>> N = Counter(t) - Counter(t_old)
>>> Nk = [N.get(i,0) for i in range(5)]
>>> Nk
[2, 1, 0, 4, 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM