我有一个字典列表(称为“入门名称”),其中包含以下信息:
{'part number': 1, 'notes': 'Fw Gibson primer on pEM113 to extract CmR resistance and pSC101 backbone and T7 promoter and term.', 'direction': 'fw primer', 'construct': '24', 'source': 'pEM113'}
{'part number': 1, 'notes': 'Re Gibson primer on pEM113 to extract CmR resistance and pSC101 backbone and T7 promoter and term.', 'direction': 're primer', 'construct': '24', 'source': 'pEM113'}
{'part number': 2, 'notes': 'Fw Gibson primer on BBa_K274100 to extract crtEBI operon', 'direction': 'fw primer', 'construct': '24', 'source': 'BBa_K274100'}
{'part number': 2, 'notes': 'Re Gibson primer on BBa_K274100 to extract crtEBI operon', 'direction': 're primer', 'construct': '24', 'source': 'BBa_K274100'}
{'part number': 1, 'notes': 'Fw Gibson primer on pEM114 to extract CmR resistance and pSC101 backbone and K1F promoter and term.', 'direction': 'fw primer', 'construct': '25', 'source': 'pEM114'}
{'part number': 1, 'notes': 'Re Gibson primer on pEM114 to extract CmR resistance and pSC101 backbone and K1F promoter and term.', 'direction': 're primer', 'construct': '25', 'source': 'pEM114'}我有另一个字典列表(称为“引物序列”),其中包含以下信息:
{'Part Number': '1', 'Construct Number': '24', 'Direction': 're primer', 'Primer Sequence': 'agaccgtcatctagtacctcTCTCCCTATAGTGAGTCGTATTACTCTAGAAGCGGCCGCg'}
{'Part Number': '1', 'Construct Number': '24', 'Direction': 'fw primer', 'Primer Sequence': 'tggaggatctgatataataaTAGCATAACCCCTTGGGGCCTCTAAACGGGTCTTGAGGGG'}
{'Part Number': '2', 'Construct Number': '24', 'Direction': 'fw primer', 'Primer Sequence': 'TACGACTCACTATAGGGAGAgaggtactagatgacggtctgcgcaaaaaaacacgttcat'}
{'Part Number': '2', 'Construct Number': '24', 'Direction': 're primer', 'Primer Sequence': 'GGCCCCAAGGGGTTATGCTAttattatatcagatcctccagcatcaaacctgctgtcgct'}
{'Part Number': '1', 'Construct Number': '25', 'Direction': 're primer', 'Primer Sequence': 'agaccgtcatctagtacctcTCTCCCTATAGTGATAGTTATTACTCTAGAAGCGGCCGCg'}
{'Part Number': '1', 'Construct Number': '25', 'Direction': 'fw primer', 'Primer Sequence': 'tggaggatctgatataataaTAGCATAACCCCTTGGGGCCTCTAAACGGGTCTTGAGGGG'}
{'Part Number': '2', 'Construct Number': '25', 'Direction': 'fw primer', 'Primer Sequence': 'TAACTATCACTATAGGGAGAgaggtactagatgacggtctgcgcaaaaaaacacgttcat'}
{'Part Number': '2', 'Construct Number': '25', 'Direction': 're primer', 'Primer Sequence': 'GGCCCCAAGGGGTTATGCTAttattatatcagatcctccagcatcaaacctgctgtcgct'}我的目标是将两者中包含的信息结合起来,这样我就可以得到一个输出,其中包含底部列表中每个引物(fw或re)的零件号、构造号、方向、引物序列、注释、构造和来源。为了匹配“引物名称”和“引物序列”,我必须检查以确保它们的“部件号”、“构造号”和“方向”都是相同的。
我尝试了以下代码来检查这一点,但它似乎不起作用:
for row in primers_names_list: #recall that primers_names_list is a list of dictionaries
if any({x['Part Number'], x['Construct Number'], x['Direction']} == {row['part number'], row['construct number'], row['direction']} for x in primers_without_names):
primers_with_names.append({'part number':row['part number'], 'construct number':row['construct number'], 'notes':row['notes'], 'primer sequence':x['Primer Sequence']})有没有人能给我一个提示,告诉我如何做到这一点?
非常感谢!
发布于 2013-01-24 04:24:29
两个问题:
part number是引物名称中的int和引物序列中的str。为了进行True比较,您必须将int转换为str (使用str(val))或将str转换为int (使用int(Val))KeyError异常,因为它们是不正确的(请注意,引物序列具有Construct Number,而引物名称具有str(Val)下面是一个有效的代码示例:
primers_names_list = [
{'part number': 1, 'notes': 'Fw Gibson primer on pEM113 to extract CmR resistance and pSC101 backbone and T7 promoter and term.', 'direction': 'fw primer', 'construct': '24', 'source': 'pEM113'},
{'part number': 1, 'notes': 'Re Gibson primer on pEM113 to extract CmR resistance and pSC101 backbone and T7 promoter and term.', 'direction': 're primer', 'construct': '24', 'source': 'pEM113'},
{'part number': 2, 'notes': 'Fw Gibson primer on BBa_K274100 to extract crtEBI operon', 'direction': 'fw primer', 'construct': '24', 'source': 'BBa_K274100'},
{'part number': 2, 'notes': 'Re Gibson primer on BBa_K274100 to extract crtEBI operon', 'direction': 're primer', 'construct': '24', 'source': 'BBa_K274100'},
{'part number': 1, 'notes': 'Fw Gibson primer on pEM114 to extract CmR resistance and pSC101 backbone and K1F promoter and term.', 'direction': 'fw primer', 'construct': '25', 'source': 'pEM114'},
{'part number': 1, 'notes': 'Re Gibson primer on pEM114 to extract CmR resistance and pSC101 backbone and K1F promoter and term.', 'direction': 're primer', 'construct': '25', 'source': 'pEM114'},
]
primers_without_names = [
{'Part Number': '1', 'Construct Number': '24', 'Direction': 're primer', 'Primer Sequence': 'agaccgtcatctagtacctcTCTCCCTATAGTGAGTCGTATTACTCTAGAAGCGGCCGCg'},
{'Part Number': '1', 'Construct Number': '24', 'Direction': 'fw primer', 'Primer Sequence': 'tggaggatctgatataataaTAGCATAACCCCTTGGGGCCTCTAAACGGGTCTTGAGGGG'},
{'Part Number': '2', 'Construct Number': '24', 'Direction': 'fw primer', 'Primer Sequence': 'TACGACTCACTATAGGGAGAgaggtactagatgacggtctgcgcaaaaaaacacgttcat'},
{'Part Number': '2', 'Construct Number': '24', 'Direction': 're primer', 'Primer Sequence': 'GGCCCCAAGGGGTTATGCTAttattatatcagatcctccagcatcaaacctgctgtcgct'},
{'Part Number': '1', 'Construct Number': '25', 'Direction': 're primer', 'Primer Sequence': 'agaccgtcatctagtacctcTCTCCCTATAGTGATAGTTATTACTCTAGAAGCGGCCGCg'},
{'Part Number': '1', 'Construct Number': '25', 'Direction': 'fw primer', 'Primer Sequence': 'tggaggatctgatataataaTAGCATAACCCCTTGGGGCCTCTAAACGGGTCTTGAGGGG'},
{'Part Number': '2', 'Construct Number': '25', 'Direction': 'fw primer', 'Primer Sequence': 'TAACTATCACTATAGGGAGAgaggtactagatgacggtctgcgcaaaaaaacacgttcat'},
{'Part Number': '2', 'Construct Number': '25', 'Direction': 're primer', 'Primer Sequence': 'GGCCCCAAGGGGTTATGCTAttattatatcagatcctccagcatcaaacctgctgtcgct'},
]
primers_with_names = []
for row in primers_names_list: #recall that primers_names_list is a list of dictionaries
for x in primers_without_names:
if (
int(x['Part Number']) == row['part number'] and
x['Construct Number'] == row['construct'] and
x['Direction'] == row['direction']
):
primers_with_names.append(
{
'part number': row['part number'],
'construct number': row['construct'],
'notes': row['notes'],
'primer sequence':x['Primer Sequence']
}
)
# If you are only expecting one match from the primers_without_names
# collection, or wish to enforce that, you can add a break statement after
# the insertion here to break out of the inner comparison loop and move on
# to the next row item
for p in primers_with_names:
print p
print
print len(primers_with_names)编辑:如果比较值对于每个集合中的每一行都是唯一的,并且如果您有足够的内存并且不介意对列表进行预处理,那么另一种选择是将两个集合转换为字典,以(零件号,构造号,方向)元组为关键字。这将以后的查找工作减少到每行摊销O(1)。总体而言,你将得到O(3N)而不是O(N^2),这对于大型集合来说是相当好的。
# convert both lists to dictionaries
primers_names_dict = {
(str(p['part number']), str(p['construct']), str(p['direction'])): p
for p in primers_names_list
}
primers_sequence_dict = {
(str(p['Part Number']), str(p['Construct Number']), str(p['Direction'])): p
for p in primers_without_names
}
# now that we have two dicts, we can do a key<->key match between them, so each
# comparison op is just a dictionary key lookup, which is O(1) on average
matches = []
for key in primers_names_dict.keys():
if key in primers_sequence_dict: # amortized O(1) lookup
matches.append( {
'part number': primers_names_dict[key]['part number'],
'construct number': primers_names_dict[key]['construct'],
'notes': primers_names_dict[key]['notes'],
'primer sequence': primers_sequence_dict[key]['Primer Sequence']
} )
for m in matches:
print m
print len(matches)发布于 2013-01-24 04:25:30
我在这里看到了两个问题。
row['construct number']放在了我认为应该是它的地方下面是修复方法:
for row in primers_names_list: #recall that primers_names_list is a list of dictionaries
for x in primers_without_names:
if {x['Part Number'], x['Construct Number'], x['Direction']} == {str(row['part number']), row['construct'], row['direction']}:
primers_with_names.append({'part number':row['part number'], 'construct number':row['construct'], 'notes':row['notes'], 'primer sequence':x['Primer Sequence']})https://stackoverflow.com/questions/14488245
复制相似问题