比较两个字典,一个是每个键的浮点值列表,另一个是每个键的值(python)

时间:2017-07-04 13:17:26

标签: python-2.7 dictionary bioinformatics

我有一个查询序列,我使用NCBIWWW.qblast在线爆破。在我的xml blast文件结果中,我为查询序列获得了一个命中列表(即:gi |)。每次击中或者gi |有多个hsp。我创建了一个字典my_dict1,我放置了gi |作为关键,我将比特分数作为值附加。每个键都有多个值。

my_dict1 = {

gi|1002819492|: [437.702, 384.47, 380.86, 380.86, 362.83],

gi|675820360| : [2617.97, 2614.37, 122.112],

gi|953764029| : [414.258, 318.66, 122.112, 86.158],

gi|675820410| : [450.653, 388.08, 386.27] }

然后我使用以下方法查找每个密钥的最大值:

for key, value in my_dict1.items():

    max_value = max(value)

并制作了第二本字典my_dict2:

my_dict2 = {

gi|1002819492|: 437.702,   

gi|675820360| : 2617.97,

gi|953764029| : 414.258,

gi|675820410| : 450.653 }

I want to compare both dictionary. So I can extract the hsp with the highest score bits. I am also including other parameters like query coverage and identity percentage (Not shown here). The finality is to get the best gi| with the highest bit scores, coverage and identity percentage.

I tried many things to compare both dictionary like this :

第一个代码:

matches[]

if my_dict1.keys() not in my_dict2.keys():

    matches[hit_id] = bit_score

else:

    matches = matches[hit_id], bit_score

第二段代码:

if hit_id not in matches.keys():

    matches[hit_id]= bit_score

else:

    matches = matches[hit_id], bit_score

第三个代码:

intersection = set(set(my_dict1.items()) & set(my_dict2.items()))

Howerver我总是遇到两种错误:

1)TypeError:list indices必须是整数,而不是unicode

2)......浮动不可迭代......

我需要一些帮助和指导。非常感谢你的时间。最好的问候。

1 个答案:

答案 0 :(得分:0)

目前尚不清楚你要做什么。什么是hit_id?什么是bit_score?如果您通过拉动第一个dict的每个键的最大值来创建第二个dict,那么您的第二个dict总是会与第一个具有相同的键。

你说你正在试图比较它们,但是并没有真正陈述你实际上要做的事情。查找价值低于特定最大值的那些?找到最高的那些?

您的第一个代码不起作用,因为我假设您尝试使用matches键值作为list的索引,您将其定义为# First off, this needs to be a dict. matches{} # This will never happen if you've created these dicts as you stated. if my_dict1.keys() not in my_dict2.keys(): matches[hit_id] = bit_score # Not clear what bit_score is? else: # Also not sure what you're trying to do here. This will assign a tuple # to matches with whatever the value of matches[hit_id] is and bit_score. matches = matches[hit_id], bit_score 。这可能是你的第一个错误来自的地方,尽管你没有给出错误实际发生的行。

请参阅下面的代码内评论:

<StackLayout x:Name="buttonStack" WidthRequest="50">
    <Image Source="fb.png" Aspect="AspectFit" HorizontalOptions="CenterAndExpand"/>
    <Label Text="Facebook" HorizontalOptions="CenterAndExpand" HorizontalTextAlignment="Center"/>
</StackLayout>

无论如何,我们真的需要更多信息和完整代码来确定您的实际目标以及出现了什么问题。