假设我有一本字典:字典一= {“ABCD”:“附件目录”、“SSEE”:“atgcccgta”、“GTAD”:“atgccggaaa”}我试过使用if和else,包括find funcion,但是我是新手,不知道如何合并所有的功能。
flmtquvp1#
{key: val for key, val in dictionaryOne.items() if "aa" in val}
The best way to filter a dictionary in Python
iq3niunx2#
您可以这样做来确定您的值是否具有aa。我假设您只需要打印bool值
aa
is_exist = False for key, val in dictionaryOne.items(): if 'aa' in val: is_exist = True print(f'{key}: {is_exist}')
更新:
# Gets user input name = input("enter the key: ") dictionaryOne = { "ABCD" : "aatttgggtcatcg", "SSEE" : "atgcccgta", "GTAD" : "atgccggaaa" } try: val = True if 'aa' in dictionaryOne.get(name) else False print(val) except Exception as k: print(k)
2条答案
按热度按时间flmtquvp1#
The best way to filter a dictionary in Python
iq3niunx2#
您可以这样做来确定您的值是否具有
aa
。我假设您只需要打印bool值更新: