在我的脚本中,我想在我的字典中包含一个键并创建一个条件,如果我已经有相同键的重复值,则不会包含它。
for seq_record in SeqIO.parse(input_file, "genbank"):
description = seq_record.description
for seq_feature in seq_record.features:
if seq_feature.type=="source":
try:
source = seq_feature.qualifiers['organism'][0]
except (KeyError, IndexError):
source = 'n/a'
try:
strain = seq_feature.qualifiers['strain'][0]
except (KeyError, IndexError):
strain = 'n/a'
if source not in dict1.keys():
dict1[source] = [value]
#print description +'\t'+ source +'\t'+ '\t'.join(value)
else:
if source in dict1.keys() and value != dict1[source]:
dict1[source].append(value)
#print description +'\t'+source +'\t'+ '\t'.join(value)
for k, v in dict1.iteritems():
print k, v
答案 0 :(得分:3)
只需将!=
替换为not in
,即:
try:
strain = seq_feature.qualifiers['strain'][0]
except (KeyError, IndexError):
strain = 'n/a'
if source not in dict1.keys():
dict1[source] = [value]
#print description +'\t'+ source +'\t'+ '\t'.join(value)
else:
if source in dict1.keys() and value not in dict1[source]:
dict1[source].append(value)
#print description +'\t'+source +'\t'+ '\t'.join(value)
答案 1 :(得分:1)
为了不向已包含此值的列表添加值,您可以使用in
运算符检查成员身份。像这样:
if not value in dict1[source]:
dict1[source].append(value)
如果您不需要将字典值作为列表,请考虑使用set,该http://server01:9090/api/v1/query_range?query=node_exporter_metrics&start=1513255115&end=1513258715&step=60已针对保持其成员的唯一性进行了优化。