且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从python中的字典中获取随机key:value对

更新时间:2022-02-09 23:43:26

如果要从字典D中获取随机的K元素,则只需使用

If you want to get random K elements from dictionary D you simply use

import random
random.sample( D.items(), K )

这就是您所需要的.

摘自Python文档:

From the Python's documentation:

随机.样本(人口 k )

返回唯一元素的 k 个长度列表 是从人口序列中选择的.用于随机抽样而无需 替换.

Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

以您的情况

import csv
import random

genes_csv = csv.reader(open('genes.csv', 'rb'))

genes_dict = {}
for row in genes_csv:
    genes_dict[row[0]] = row[1:]

length = raw_input('How many genes do you want? ')
random_list = random.sample( genes_dict.items(), int(length) )
print random_list

无需遍历字典的所有键

for key in genes_dict:
    random_list = random.sample(genes_dict.items(), int(length))
    print random_list

注意,实际上您实际上没有在循环内使用key变量,这应该警告您在这里可能有问题.尽管返回每个可能的100个基因的组合"是不正确的.但是,它只是返回N个随机的k个元素基因列表(在您的情况下为100个),其中N是字典的大小,远非所有组合"(N!/(N-k)!k!)

notice, that you are actualy not using the key variable inside your loop, which should warn you that something may be wrong here. Although it is not true that it " return every possible combination of 100 genes.", it simply returns N random k element genes lists (in your case 100), where N is the size of the dictionary, which is far from being "all combinations" (which is N!/(N-k)!k!)