Get random key: pairs of values ​​from a dictionary in python

I am trying to get a random set of key-value pairs from a dictionary that I made from a csv file. The dictionary contains information about the genes, the gene name being the key of the dictionary, and the list of numbers (related to gene expression, etc.) is 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 that if I try to get a list of 100 genes (for example), it seems to iterate over the entire dictionary and return all possible combinations of 100 genes.

+4
source share
3 answers

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

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

and all you need.

From the Python documentation:

random pattern (population, k)

Returns a list of length k unique elements selected from a 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 

No need to iterate over all dictionary keys

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

note that you are not actually using the key variable inside your loop, which should warn you that something might be wrong here. Although it is not true that it "returns all possible combinations of 100 genes," it simply returns N random K lists of element genes (in your case 100), where N size of a dictionary that is far from "all combinations" (which is N!/(Nk)!k! )

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

It goes through each key, and a pattern is printed for each key. You are looking just

 random_list = random.sample(genes_dict.items(), int(length)) print random_list 
+2
source

I agree with others without iteration with dictionary keys. However, if you do not want the format returned as a tuple, but as a list you can use

  random_list=genes_dict.keys() int(length) 

Then, to get the values, you need a loop if "int (length)" is more than one:

  for x in random_list: print x,genes_dict[x] //or to create a new dict of the random values you could random_genes_dict[x]=genes_dict[x] 
0
source

All Articles