Python 映射列表中的字典键值

Python 映射列表中的字典键值,python,list,dictionary,Python,List,Dictionary,假设我有列表t=[3,3,4,0,0,3,1,0,4,3,3,3,0],它的子集是 t_old=[3,3,4,0,0]现在我必须计算不在t_old[3,1,0,4,3,3,0]中的项目的频率,我已经计算了N={0:2,1:1,3:3,4:1},但是在那之后,我必须将这些值映射到一个大小为5的列表Nk中,这样N={0:2,1:1,3:4,1},所以列表将是Nk=[2,1,0,4,1] 0->2,1->1,2->0,因为没有2,3->3,4->1的频率,所以NK是[2,1,0,4,1] 此外,顺序也

假设我有列表t=[3,3,4,0,0,3,1,0,4,3,3,3,0],它的子集是 t_old=[3,3,4,0,0]现在我必须计算不在t_old[3,1,0,4,3,3,0]中的项目的频率,我已经计算了N={0:2,1:1,3:3,4:1},但是在那之后,我必须将这些值映射到一个大小为5的列表Nk中,这样N={0:2,1:1,3:4,1},所以列表将是Nk=[2,1,0,4,1] 0->2,1->1,2->0,因为没有2,3->3,4->1的频率,所以NK是[2,1,0,4,1] 此外,顺序也很重要 我的代码是

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)
所以我想要以列表的形式输出

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

我怎样才能得到输出?请提前感谢

,你的问题是什么?你能进一步解释为什么N等于{0:2,1:1,3:3,4:1}?t有六个三,t_old有两个三,所以N不应该有四个三吗?不在t_old中的项=[1,0,4,3,3,0]它们是不在t_old中的项。所以三的频率显然是3@kevinI不明白。为什么[1,0,4,3,3,3,0]被认为是不在t_old中的项目?为什么不是[3,1,0,4,3,3,3,0]?t_old有五个元素长,那么为什么我们要省略t的前六个元素呢?@RafaelC,你同意我的说法吗?你的问题是什么?你能进一步解释为什么N等于{0:2,1:1,3:4,4:1}吗?t有六个三,t_old有两个三,所以N不应该有四个三吗?不在t_old中的项=[1,0,4,3,3,0]它们是不在t_old中的项。所以三的频率显然是3@kevinI不明白。为什么[1,0,4,3,3,3,0]被认为是不在t_old中的项目?为什么不是[3,1,0,4,3,3,3,0]?t_old有五个元素长,那么为什么我们要省略t的前六个元素呢?@RafaelC,你同意我的说法吗?counter-counter是{0:2,1:1,3:4,4:1},而不是{0:2,1:1,3:3,4:1}
>>> 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]