我正在从事一个项目,在该项目中,我必须搜索文本字符串中的各种子字符串,我发现自己遇到了一个问题,即当搜索的字符串拥有所有可能的“子字符串”时,它运行良好,我可以评估数据,但当它只有部分子字符串在6个选项中时,即3个选项,那就不行了
下面是C语言的代码
with open(argv[1], "r") as file:
reader = csv.DictReader(file)
valid = False
for row in reader:
if str_count['AGATC'] == int(row['AGATC']) and str_count['TTTTTTCT'] == int(row['TTTTTTCT']) and str_count['AATG'] == int(row['AATG']) and str_count['TCTAG'] == int(row['TCTAG']) and str_count['GATA'] == int(row['GATA']) and str_count['TATC'] == int(row['TATC']) and str_count['GAAA'] == int(row['GAAA']) and str_count['TCTG'] == int(row['TCTG']):
print(row['name'])
valid = True
if valid == False:
print("No match")
问题是,如果我处理的是一个只有部分选项的字符串,我如何才能让它发挥作用。例如,如果我有一个字符串,如:
name,AGATC,TTTTTTCT,AATG,TCTAG,GATA,TATC,GAAA,TCTG
但是,如果我具备以下条件,它工作得很好:
TTTTTTCT,AGATC,AATG,TATC
我希望避免硬编码,我将感谢任何可能的帮助
使用Python all函数
更换
与
解释
row是基于标题(第一行)的k,v对字典
例如,如果标题行是:name、AGATC、AATG、TATC,则每一行将是:
其中,v1、v2、v3、v4因行而异,但键始终相同
表达方式:
是字典的键、值对(元组)列表,如中所示:
想要在这些元组上循环以查找str_count(k)==int(vi),除了k==name之外
使用列表理解获取所有元组,其中k=='name'除外
要检查此列表中的所有k,v对是否满足条件
切换到生成器而不是列表
相关问题 更多 >
编程相关推荐