Python 如何从np.unique、zip和Comprehension创建dict?

Python 如何从np.unique、zip和Comprehension创建dict?,python,numpy,dictionary,zip,Python,Numpy,Dictionary,Zip,为什么dw的长度小于24? 我想测试pytorch的WeightedRandomSampler,所以在这段代码中,我打印源频率,然后将其与批次频率进行比较 import numpy as np data = np.array(list((*np.random.randint(3,size=20), *np.random.randint(90,100,size=4)))) freq = dict(zip(*np.unique(data, return_counts=True))) weights=

为什么dw的长度小于24? 我想测试pytorch的WeightedRandomSampler,所以在这段代码中,我打印源频率,然后将其与批次频率进行比较

import numpy as np
data = np.array(list((*np.random.randint(3,size=20), *np.random.randint(90,100,size=4))))
freq = dict(zip(*np.unique(data, return_counts=True)))
weights=[1/(1 + freq[x]) for x in data]
dw = dict(zip(data, weights))
print(dw, len(dw))
对不起,我的钥匙丢失了)。
dict的长度小于len(数据),因为数据用作键,并且数据中存在重复元素。我应该只使用zip。

如果我使用dw=dict(zip(data,range(len(weights))),输出也很奇怪:{1:19,0:16,2:18,99:21,90:22,94:23}。我是否误解了np.unique函数?不清楚您到底想要什么?我想为torch…WeightedRandomSampler制作重量。所以我试着对一些不一致的序列进行抽样,并从中以不同的概率得到批次。
dw=zip(data,weights)
print(list(dw))