I working this CS50 problem set that tell us to match people DNA
This is my almost finished code:
import re, csv, sys
def main(argv):
# Open csv file
csv_file = open(sys.argv[1], 'r')
people = csv.reader(csv_file)
nucleotide = next(people)[1:]
# Open dna sequences file
txt_file = open(sys.argv[2], 'r')
dna_file = txt_file.read()
str_repeat = {}
str_list = find_STRrepeats(str_repeat, nucleotide, dna_file)
match_dna(people, str_list)
def find_STRrepeats(str_list, nucleotide, dna):
for STR in nucleotide:
groups = re.findall(rf'(?:{STR})+', dna)
if len(groups) == 0:
str_list[STR] = 0
else:
total = max(len(i)/len(STR) for i in groups)
str_list[STR] = int(total)
return str_list
def match_dna(people, str_list):
for row in people:
# Get people name in people csv
person = row[0]
# Get all dna value of each people
data = row[1:]
# If all value in dict equal with all value in data, print the person
if str_list.values() == data:
print(person)
sys.exit(0)
print("No match")
if __name__ == "__main__":
main(sys.argv[1:])
So, i have stuck on my match_dna
function. I get confused on how to compare value in my dict: str_list
with value in list: people
.
str_list = {'AGATC': 4, 'AATG': 1, 'TATC': 5}
data = ['4', '1', '5']
Is there anything i should change in my code? or maybe a simple way to compare those two different structures? thx.
str_list = {'AGATC': 4, 'AATG': 1, 'TATC': 5}
data = ['4', '1', '5']
for data_item in data:
for key,values in str_list.items():
# list data '4','1','5' are in string
# and dictonery value 4,1,5 are in integer form
# hence you need to compare the same data type
if values == int(data_item):
print(key)
In your second snipped provided by you. List data ie, "data" '4','1','5' is in the string and dictionary value ie, "str_list" 4,1,5 are in integer form hence you need to compare the same data type by converting list data to an integer. You can view the above code by me for your reference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.