python随机选择字典元素,从python中的字典获取随机key:value对

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

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.

解决方案

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

import random

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

and that's all you need.

From the Python's documentation:

random.sample(population, k)

Return a k length list of unique elements

chosen from the population sequence. Used for random sampling without

replacement.

In your case

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

There is no need to iterate through all the keys of the dictionary

for key in genes_dict:

random_list = random.sample(genes_dict.items(), int(length))

print random_list

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!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值