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

时间:2021-05-18 14:17:36

I have a query sequence that I blasted online using NCBIWWW.qblast. In my xml blast file result I obtained for a query sequence a list of hit (i.e: gi|). Each hit or gi| have multiple hsp. I made a dictionary my_dict1 where I placed gi| as key and I appended the bit score as value. So multiple values for each key.

我有一个查询序列,我使用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] }

Then I looked for max value in each key using:

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

for key, value in my_dict1.items():

    max_value = max(value)

And made a second dictionary my_dict2:

并制作了第二个字典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 :

First code :

第一个代码:

matches[]

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

    matches[hit_id] = bit_score

else:

    matches = matches[hit_id], bit_score

Second code:

第二个代码:

if hit_id not in matches.keys():

    matches[hit_id]= bit_score

else:

    matches = matches[hit_id], bit_score

Third code:

第三个代码:

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

Howerver I always end up with 2 types of errors:

Howerver我总是遇到两种类型的错误:

1 ) TypeError: list indices must be integers, not unicode

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

2 ) ... float not iterable...

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

Please I need some help and guidance. Thank you very much in advance for your time. Best regards.

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

1 个解决方案

#1


0  

It's not clear what you're trying to do. What is hit_id? What is bit_score? It looks like your second dict is always going to have the same keys as your first if you're creating it by pulling the max value for each key of the first dict.

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

You say you're trying to compare them, but don't really state what you're actually trying to do. Find those with values under a certain max? Find those with the highest max?

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

Your first code doesn't work because I'm assuming you're trying to use a dict key value as an index to matches, which you define as a list. That's probably where your first error is coming from, though you haven't given the lines where the error is actually occurring.

您的第一个代码不起作用,因为我假设您正在尝试使用dict键值作为匹配的索引,您将其定义为列表。这可能是您的第一个错误来自的地方,尽管您没有给出错误实际发生的行。

See in-code comments below:

请参阅下面的代码内注释:

# 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

Regardless, we really need more information and the full code to figure out your actual goal and what's going wrong.

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

#1


0  

It's not clear what you're trying to do. What is hit_id? What is bit_score? It looks like your second dict is always going to have the same keys as your first if you're creating it by pulling the max value for each key of the first dict.

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

You say you're trying to compare them, but don't really state what you're actually trying to do. Find those with values under a certain max? Find those with the highest max?

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

Your first code doesn't work because I'm assuming you're trying to use a dict key value as an index to matches, which you define as a list. That's probably where your first error is coming from, though you haven't given the lines where the error is actually occurring.

您的第一个代码不起作用,因为我假设您正在尝试使用dict键值作为匹配的索引,您将其定义为列表。这可能是您的第一个错误来自的地方,尽管您没有给出错误实际发生的行。

See in-code comments below:

请参阅下面的代码内注释:

# 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

Regardless, we really need more information and the full code to figure out your actual goal and what's going wrong.

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