从python中的字典中获取随机key:value对 [英] Get random key:value pairs from dictionary in python

查看:1140
本文介绍了从python中的字典中获取随机key:value对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我从csv文件制作的字典中抽出一组随机的键/值对.字典包含有关基因的信息,其中基因名称为字典关键字,数字列表(与基因表达等有关)为值.

I'm trying to pull out a random set of key-value pairs from a dictionary I made from a csv file. The dictionary contains information for genes, with the gene name being the dictionary key, and a list of numbers (related to gene expression etc.) being the value.

# python 2.7.5
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? ')

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

问题是,如果我尝试获取100个基因的列表(例如),它似乎遍历整个字典并返回100个基因的每种可能组合.

The problem is, if I try to get a list of 100 genes (for example), it seems to iterate over the whole dictionary and return every possible combination of 100 genes.

推荐答案

如果要从字典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!)

这篇关于从python中的字典中获取随机key:value对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆