使用列表推导来匹配存储在列表中的字典的值 [英] Using list comprehension to match values of dictionaries which are stored in lists

查看:110
本文介绍了使用列表推导来匹配存储在列表中的字典的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字典列表需要比较:

I have two lists of dictionaries which I need to compare:

search_list =[{'Chr':'chr1','St':'2345','End':'2456'},
              {'Chr':'chr1','St':'3457','End':'4567'}]

database = [{'Chr':'chr1','St':'2348','End':'2348'},
            {'Chr':'chr1','St':'190','End':'190'}]

因此,列表中的每个字典如下:

Therefore each dictionary within the list looks like this:

{'Chr':'chr1','St':'2345','End':'2456'}

其中Chr =染色体,St =起点坐标,End =终点坐标.

Where Chr = Chromosome, St = start coordinate, and End = end coordinate.

我需要在数据库中标识与Chr值匹配并且在St和End值之间的值(该值必须是整数,而不是它们的当前字符串形式).据我所知,列表理解是实现此目标的最佳方法.查看这个问题是我的概念起点,但是我对于如何进行操作有些困惑.

I need to identify the values in the database which match on the Chr value and are between the St and End values (which need to be integers, rather than their current string form). From what I can tell list comprehension is the best way to accomplish this. Looking at this question has been my conceptual starting point, but I'm a bit confused about how to proceed.

我的伪代码是:

matched = [var for var in search_list where search_list['Chr'] == database['Chr'] AND search_list['St'] >= database['St'] <= search_list['End']]

但是显然这很粗糙!任何建议都将受到欢迎.

But obviously that's very rough! Any suggestions would be most welcome.

更新我尝试了KobiK的建议,该建议似乎可行.但是现在很难将"St"和"End"键上附加的值从字符串转换为整数,以便进行数学搜索.

Update I've tried KobiK's suggestion which looks like it should work. But now am having trouble transforming the values attached to the 'St' and 'End' keys from strings to integers in order to do the mathematical searching.

这就是我所拥有的:

matchedVars ={[record for record in database for lookup in search_list if 
record['Chr'] == lookup['Chr'] if int(lookup['St']) <= int(record['St']) 
<= int(lookup['End'])]}

但是它抛出了这个错误:

But it throws this error:

matchedVars = {[记录 如果record ['Chr'] ==,则在数据库中进行记录以在search_list中查找 lookup ['Chr']如果int(lookup ['St'])< = int(record ['St'])< = int(lookup ['End'])]}

matchedVars ={[record for record in database for lookup in search_list if record['Chr'] == lookup['Chr'] if int(lookup['St']) <= int(record['St']) <= int(lookup['End'])]}

TypeError:字符串索引必须为整数

TypeError: string indices must be integers

是否有更好/更合适的方法将键从字符串转换为整数?

Is there a better/more appropriate way to transform keys from strings to ints?

推荐答案

您可以使用列表理解力: 此代码将迭代database dict中的元素,并将检查search_list

You can use list comprehension: This code will iterate elemnts in database dict and will check for criteria in search_list

search_list =[{'Chr':'chr1','St':'2345','End':'2456'},{'Chr':'chr2','St':'3457','End':'4567'}]
database = [{'Chr':'chr1','St':'2348','End':'2348'},{'Chr':'chr2','St':'190','End':'190'}]

print [record for record in database for lookup in search_list
       if record['Chr'] == lookup['Chr'] if lookup['St'] <= record['St'] <= lookup['End']]

输出:

[{'Chr': 'chr1', 'End': '2348', 'St': '2348'}]

正如您在输出中看到的,只有{'Chr': 'chr1', 'End': '2348', 'St': '2348'}search_list

As you can see in the output only {'Chr': 'chr1', 'End': '2348', 'St': '2348'} is matching the criteria's inside the search_list

这篇关于使用列表推导来匹配存储在列表中的字典的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆