简体   繁体   English

在包含字符串的字典列表中查找整数列表

[英]Finding a list of integers in a list of dictionaries containing strings

I am working on a problem for a class. We are given a long string of DNA (AGATC...) and are expected to find a match for several Short Tandem Repeats of a small subequence like AGA appearing 10 times, GTC appearing 4 times and so on.我正在研究一个 class 的问题。我们得到一长串 DNA(AGATC...),预计会找到一个小子序列的几个短串联重复序列的匹配项,例如 AGA 出现 10 次,GTC 出现 4 次等等。 The CSV file containing the Short Tandem Repeats looks sort of like this with the header row at the top:包含短串联重复的 CSV 文件看起来有点像这样,顶部有 header 行:

name, AGA, GTCC, ATTT

Ted, 4, 5, 9

Bill, 3, 2, 8

Depending on the file I use, there can be more or fewer individual repeats to look for (3 or 8) .根据我使用的文件,可以查找更多或更少的单个重复(3 or 8) I have distilled the matching Short Tandem Repeats found in the long String down to a list of integers stored in a list variable.我已经将在长字符串中找到的匹配短串联重复提炼为存储在列表变量中的整数列表。 I get this from a text file (the string of DNA) read into memory and then analyzed using another file read into memory, the CSV file I just mentioned.我从读入 memory 的文本文件(DNA 字符串)中得到这个,然后使用另一个读入 memory 的文件进行分析,即我刚才提到的 CSV 文件。 What I want to do is locate the presence of the list of integers (representing the number of times each Short Tandem Repeat appears in the long string of DNA) in the CSV file which I have also stored into a variable as a list of dictionaries (using Dictreader ) like [{'name': 'Ted', 'AGA': '4', 'GTCC': '5', 'ATTT': '9'}, {'name': 'Ted', 'AGA': '3', 'AGA': '2', 'GTCC': '8'}] .我想要做的是在 CSV 文件中找到整数列表(表示每个短串联重复出现在长串 DNA 中的次数),我也将其作为字典列表存储到变量中(使用Dictreader ) like [{'name': 'Ted', 'AGA': '4', 'GTCC': '5', 'ATTT': '9'}, {'name': 'Ted', 'AGA': '3', 'AGA': '2', 'GTCC': '8'}]

How can I find a list of integers like [4, 5, 9] inside my list of dictionaries matching Ted's Short Tandem Repeats and then print out Ted's name as well?我怎样才能在我的字典列表中找到像[4, 5, 9]这样的整数列表来匹配 Ted 的 Short Tandem Repeats 然后打印出 Ted 的名字? Please help.请帮忙。

This is simple operation indeed reading from the csv except the header and appeding it to a list.这是一个简单的操作,实际上是从 csv 中读取数据,但 header 除外,并将其添加到列表中。

import csv

value = []
with open('file.csv', 'r') as file:
    data = csv.reader(file)
    header = next(data)
    for row in data:
        # print(row)
        value.append(row)
print(value[0])
print(value[1])

Output: Output:

['Ted', ' 4', ' 5', ' 9']
['Bill', ' 3', ' 2', ' 8']

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

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