【发布时间】:2021-11-28 07:19:23
【问题描述】:
这是针对 CS50 课程的一项名为 DNA in python 的作业,我已经苦苦研究了好几天,并试图弄清楚如何让最后一部分发挥作用。我是新手。
我已将人员及其 DNA 的数据库上传到内存中的字典列表中。
然后,我将 DNA 的测试样本作为字符串读入内存,然后搜索并过滤它以寻找 DNA 序列。
所以我现在拥有的是一个名为 str_test 的字典和一个名为 data 的字典列表,其中包含每个人的 DNA,我需要以某种方式将 str_test 与数据进行比较,以查看是否有人匹配并返回人名。
就像我说的那样,我正在努力解决这个问题,我已经研究了如何循环和处理数据中的值 - dict 列表以及 str_test 结果的dict,但我无法将它们混合在一起。
对于#d out 区域的数量,我深表歉意,但它们是供我测试的,任何指导都将不胜感激。最后 5 到 7 行是我试图循环的,但这是错误的,必须有一个简单的更好的方法,谢谢
import csv
import sys
def main():
# Ensure correct usage
if len(sys.argv) != 3:
sys.exit("Usage: python dna.py data.csv sequence.txt")
data = []
with open(sys.argv[1], "r") as csvfile: #open the file in the command line argument
reader = csv.DictReader(csvfile)
for row in reader: #lets go loopy
row = (row)
data.append(row)
#print(row)
#print(data)
#print(reader.fieldnames)
#print(data)
with open(sys.argv[2], "r") as file: #open the sample file in the command line argument
sequence = file.read() #read it to array/memory
print(reader.fieldnames) #test print
print(sequence) #test print
#two files now opened
#sequence is the test DNA sequence
#we want to loop through the STR's here and take the first and then loop that through the sequence
str_test = {} # a dictionary for the counts
for i in range (1, (len(reader.fieldnames ))): #read the header field names ie STR, count them starting after name field
sample = (reader.fieldnames[i])
print(reader.fieldnames)
print(sample) #DNA type to compare to test string called SEQUENCE
str_test[sample] = 0
for j in range(len(sequence)): # this will loop through the long string to be tested for the STR DNA (sample)
step = 0
max_count = 0
while sequence[j + step:j+step+len(sample)] == sample:
step = step + len(sample)
max_count += 1
print(max_count)
j = j + step
#test = (str_test.get(sample))
if max_count > (str_test.get(sample)): #get the existing value of the sample and compare
str_test[sample] = max_count #if count is larger then update field if not continue
print(str_test) #test print to see whats in the dictionary
print(str_test.values())
#print(key.values())
#print(data)
#print(len(data))
#print(type(str_test))
for d in data: #data is a list of dictionaries - so this is cycle through the list [1:] to start at first
for values in (str_test): #start to loop through the dictionary for the test string dna results
for key in d: #this is cycling through the dictionary thats part of the list of dictionaries
strvalue = str_test.get(values)
datavalue = int(d.get(values)
main()
str_test 中包含的信息是这样的 -
{'AGATC': 4, 'TTTTTTCT': 0, 'AATG': 1, 'TCTAG': 0, 'GATA': 1, 'TATC': 5, 'GAAA': 1, 'TCTG': 0}
我必须搜索与上述 str_test 匹配的 dict 列表中的信息如下所示:
[
{
'name': 'Albus',
'AGATC': '15',
'TTTTTTCT': '49',
'AATG': '38',
'TCTAG': '5',
'GATA': '14',
'TATC': '44',
'GAAA': '14',
'TCTG': '12'
},
{
'name': 'Cedric',
'AGATC': '31',
'TTTTTTCT': '21',
'AATG': '41',
'TCTAG': '28',
'GATA': '30',
'TATC': '9',
'GAAA': '36',
'TCTG': '44'
},
{
'name': 'Draco',
'AGATC': '9',
'TTTTTTCT': '13',
'AATG': '8',
'TCTAG': '26',
'GATA': '15',
'TATC': '25',
'GAAA': '41',
'TCTG': '39'
},
{
'name': 'Fred',
'AGATC': '37',
'TTTTTTCT': '40',
'AATG': '10',
'TCTAG': '6',
'GATA': '5',
'TATC': '10',
'GAAA': '28',
'TCTG': '8'
},
{
'name': 'Ginny',
'AGATC': '37',
'TTTTTTCT': '47',
'AATG': '10',
'TCTAG': '23',
'GATA': '5',
'TATC': '48',
'GAAA': '28',
'TCTG': '23'
},
{
'name': 'Hagrid',
'AGATC': '25',
'TTTTTTCT': '38',
'AATG': '45',
... # this is a short extract
【问题讨论】:
-
只是一些简短的笔记。
sys.exit不打印其结果。它接受一个作为返回码返回的整数。在那里使用print。其次,row = (row)绝对什么都不做。反正你不需要它。 -
所以,你知道如何做一个循环。
for row in data:/if sequence in row['dna']:似乎是您所需要的。你没有向我们展示数据,所以我们不知道这些列是什么。 -
{'AGATC': 4, 'TTTTTTCT': 0, 'AATG': 1, 'TCTAG': 0, 'GATA': 1, 'TATC': 5, 'GAAA': 1 , 'TCTG': 0} 是过滤后需要匹配的字符串,listofdict 是 [{'name': 'Albus', 'AGATC': '15', 'TTTTTTCT': '49', 'AATG': '38','TCTAG':'5','GATA':'14','TATC':'44','GAAA':'14','TCTG':'12'},{'name': 'Cedric','AGATC':'31','TTTTTTCT':'21','AATG':'41','TCTAG':'28','GATA':'30','TATC':'9 ', 'GAAA': '36', 'TCTG 等,这是一个小摘录,但会给你一个想法,谢谢你的任何建议,C 是合乎逻辑的 Python 让我在寻址位置时感到困惑,谢谢
-
我只是查看 if 行中的序列,我想我需要.... if str_test in row['data'} 命令,仍在尝试解决此语法,我的数据文件在它的“名称”中有 dict 键,其中测试文件不只是直接键:值,这对行中的 if 序列有何影响?谢谢 Tim
-
鉴于您的数据,您对问题的描述完全没有意义。列表中的每个名称都包含搜索列表中的每个序列。我可以告诉你一个答案,但问题肯定比你分享的更多。
标签: python list dictionary