简体   繁体   English

获取嵌套字典中值的中位数

[英]Get median of values in nested dictionary

I have a nested dictionary with the following structure: 我有一个具有以下结构的嵌套字典:

Clusters{Cluster_X :{accession1: 8, accession2: 3, accession3: 7}, Cluster_Y :{accession1: 7.....}} 

The dictionary represents clusters of DNA sequences with accession numbers as the key for the sequences length. 字典代表了DNA序列簇,其登录号是序列长度的关键。 Now, for each subdictionary in the dictonary , I want to extract the key which represents the median of the sequence lengths for the respective subdictionary. 现在,对于字典中的每个子字典,我要提取代表相应子字典序列长度中值的键。

Does anybody know how to do that? 有人知道该怎么做吗?

It is just a sample to print the medians and corresponding keys for each sub-dictionary. 它只是打印每个子词典的中位数和相应键的示例。 In for loop we construct a list l which consists of pairs of a sequence length and a key. 在for循环中,我们构造了一个列表l ,该列表由一对序列长度和一个键组成。 Then l is sorted by the lengths. 然后按长度对l进行排序。

Clusters = {"Cluster_X" :{"accession1": 8, "accession2": 3, "accession3": 7}," Cluster_Y" :{"accession1": 7, "accession2": 10}} 

for c in Clusters:
    l = [[m,k] for k, m in Clusters[c].items()]
    l.sort()
    print 'median:', l[len(l)//2][0], ' key:', l[len(l)//2][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM