如何将一本字典与另一本字典的一部分进行比较?
How to compare one dictionary to part of another dictionary?
我正在检查 CS50 的 pset6 dna。
我在程序的最后一部分遇到问题,我将字典与字典列表的一部分进行比较。
代码:
import csv
import sys
#ensure csv input file
if len(sys.argv) != 3:
sys.exit("Include csv and txt file")
dna=[]
#open and read csv
file = open(sys.argv[1],"r")
reader = csv.DictReader(file)
for row in reader:
dna.append(row)
sequence = ""
#open and read txt
file = open(sys.argv[2], "r")
sequence = file.read()
str_list = []
str_list = reader.fieldnames[1:]
seq_count_list = []
seq_count = 0
seq_master_list = {}
#count consecutive STR in sequence
for STR in str_list:
STR_original = STR
str_length = len(STR)
while STR in sequence:
seq_count += 1
STR += STR_original
seq_count_list.append(seq_count)
seq_master_list[STR[0:str_length]] = str(seq_count)
seq_count = 0
#unsure how to use this list of names
dna_names = []
for i in dna:
dna_names.append(i['name'])
#check if sequence matches anyone in dna csv
for row in dna:
print(row)
if seq_master_list in row:
print("FOUND")
print(seq_master_list)
具体代码:
for row in dna:
print(row)
if seq_master_list in row:
print("FOUND")
它试图匹配字典 seq_master_list
:
{'AGATC': '22', 'TTTTTTCT': '33', 'AATG': '43', 'TCTAG': '12', 'GATA': '26', 'TATC': '18', 'GAAA': '47', 'TCTG': '41'}
字典列表 dna
看起来像:
[
...
{
'name': 'Kingsley', 'AGATC': '7', 'TTTTTTCT': '11', 'AATG': '18', 'TCTAG': '33', 'GATA': '39', 'TATC': '31', 'GAAA': '23', 'TCTG': '14'
},
{
'name': 'Lavender', 'AGATC': '22', 'TTTTTTCT': '33', 'AATG': '43', 'TCTAG': '12', 'GATA': '26', 'TATC': '18', 'GAAA': '47', 'TCTG': '41'
},
{
'name': 'Lily', 'AGATC': '42', 'TTTTTTCT': '47', 'AATG': '48', 'TCTAG': '18', 'GATA': '35', 'TATC': '46', 'GAAA': '48', 'TCTG': '50'
},
...
]
现在我收到错误:
TypeError: unhashable type: 'dict'
我希望它吐出 'name'
,在本例中为 'Lavender'
。
编辑:
根据来自 JohnGordon 的 Inline Link。
for row in dna:
print(row)
if seq_master_list.items() in row.items():
print("FOUND")
我没有收到任何错误...但似乎没有(没有打印出“FOUND”)
感谢 Chris Charley 的解决方案
for STR in str_list:
STR_original = STR #I think this part should be changed. Why did you indicate STR_original and use STR below line?
str_length = len(STR)
while STR in sequence:
seq_count += 1
STR += STR_original
seq_count_list.append(seq_count)
seq_master_list[STR[0:str_length]] = str(seq_count)
seq_count = 0
要让字典比较相等,您需要从 row
字典中获取 name
键和值。如果 'name' 键和值仍在 row
字典中,它永远不会 ==
seq_master_list
字典中。
found = False
for row in dna:
name = row.pop('name') # remove this from the 'row' dict and save the name
if row == seq_master_list:
found = True
print(name)
break
if not found:
print('no match')
我正在检查 CS50 的 pset6 dna。 我在程序的最后一部分遇到问题,我将字典与字典列表的一部分进行比较。
代码:
import csv
import sys
#ensure csv input file
if len(sys.argv) != 3:
sys.exit("Include csv and txt file")
dna=[]
#open and read csv
file = open(sys.argv[1],"r")
reader = csv.DictReader(file)
for row in reader:
dna.append(row)
sequence = ""
#open and read txt
file = open(sys.argv[2], "r")
sequence = file.read()
str_list = []
str_list = reader.fieldnames[1:]
seq_count_list = []
seq_count = 0
seq_master_list = {}
#count consecutive STR in sequence
for STR in str_list:
STR_original = STR
str_length = len(STR)
while STR in sequence:
seq_count += 1
STR += STR_original
seq_count_list.append(seq_count)
seq_master_list[STR[0:str_length]] = str(seq_count)
seq_count = 0
#unsure how to use this list of names
dna_names = []
for i in dna:
dna_names.append(i['name'])
#check if sequence matches anyone in dna csv
for row in dna:
print(row)
if seq_master_list in row:
print("FOUND")
print(seq_master_list)
具体代码:
for row in dna:
print(row)
if seq_master_list in row:
print("FOUND")
它试图匹配字典 seq_master_list
:
{'AGATC': '22', 'TTTTTTCT': '33', 'AATG': '43', 'TCTAG': '12', 'GATA': '26', 'TATC': '18', 'GAAA': '47', 'TCTG': '41'}
字典列表 dna
看起来像:
[
...
{
'name': 'Kingsley', 'AGATC': '7', 'TTTTTTCT': '11', 'AATG': '18', 'TCTAG': '33', 'GATA': '39', 'TATC': '31', 'GAAA': '23', 'TCTG': '14'
},
{
'name': 'Lavender', 'AGATC': '22', 'TTTTTTCT': '33', 'AATG': '43', 'TCTAG': '12', 'GATA': '26', 'TATC': '18', 'GAAA': '47', 'TCTG': '41'
},
{
'name': 'Lily', 'AGATC': '42', 'TTTTTTCT': '47', 'AATG': '48', 'TCTAG': '18', 'GATA': '35', 'TATC': '46', 'GAAA': '48', 'TCTG': '50'
},
...
]
现在我收到错误:
TypeError: unhashable type: 'dict'
我希望它吐出 'name'
,在本例中为 'Lavender'
。
编辑:
根据来自 JohnGordon 的 Inline Link。
for row in dna:
print(row)
if seq_master_list.items() in row.items():
print("FOUND")
我没有收到任何错误...但似乎没有(没有打印出“FOUND”)
感谢 Chris Charley 的解决方案
for STR in str_list:
STR_original = STR #I think this part should be changed. Why did you indicate STR_original and use STR below line?
str_length = len(STR)
while STR in sequence:
seq_count += 1
STR += STR_original
seq_count_list.append(seq_count)
seq_master_list[STR[0:str_length]] = str(seq_count)
seq_count = 0
要让字典比较相等,您需要从 row
字典中获取 name
键和值。如果 'name' 键和值仍在 row
字典中,它永远不会 ==
seq_master_list
字典中。
found = False
for row in dna:
name = row.pop('name') # remove this from the 'row' dict and save the name
if row == seq_master_list:
found = True
print(name)
break
if not found:
print('no match')