【发布时间】:2020-03-23 18:53:25
【问题描述】:
我想检查这些值存在于什么组合中,一个条件是主题标签不能单独退出,因此构成这些条件语句的组合是 4C1 + 5C2 + 5C3 + 5C4 + 5C5。
我想知道在python中使用字典方法是否有更有效的方法。
我的字典
data = { 'all_words': '', 'exact_phrase': '', 'any_words': '', 'not_words': '', 'hashtag': ''}
我的条件语句
if data['all_words'] is not None and (data['any_words'], data['exact_phrase'], data['not_words'], data['hashtag'] is None):
c.Search = data['all_words']
elif data['any_words'] is not None and (data['all_words'], data['exact_phrase'], data['not_words'], data['hashtag'] is None):
c.Search = data['any_words']
elif data['exact_phrase'] is not None and (data['all_words'], data['any_words'], data['not_words'], data['hashtag'] is None):
c.Search = data['exact_phrase']
elif data['hashtag'] is not None and (data['all_words'], data['any_words'], data['not_words'], data['exact_phrase'] is None):
c.Search = data['hashtag']
elif (data['all_words'], data['any_words'] is not None) and (data['exact_phrase'], data['not_words'], data['hashtag'] is None):
c.Search = data['all_words'] + " " + data['any_words']
elif (data['all_words'], data['exact_phrase'] is not None) and (data['any_words'], data['not_words'], data['hashtag'] is None):
c.Search = data['all_words'] + " " + data['exact_phrase']
elif (data['all_words'], data['not_words'] is not None) and (data['any_words'], data['exact_phrase'], data['hashtag'] is None):
c.Search = data['all_words'] + " " + data['not_words']
elif (data['all_words'], data['hashtag'] is not None) and (data['any_words'], data['exact_phrase'], data['not_words'] is None):
c.Search = data['all_words'] + " " + data['hashtag']
elif (data['any_words'], data['exact_phrase'] is not None) and (data['all_words'], data['hashtag'], data['not_words'] is None):
c.Search = data['exact_phrase'] + " " + data['any_words']
elif (data['any_words'], data['hashtag'] is not None) and (data['all_words'], data['exact_phrase'], data['not_words'] is None):
c.Search = data['any_words'] + " " + data['hashtag']
elif (data['any_words'], data['not_words'] is not None) and (data['all_words'], data['exact_phrase'], data['hashtag'] is None):
c.Search = data['any_words'] + " " + data['not_words']
elif (data['exact_phrase'], data['hashtag'] is not None) and (data['all_words'], data['any_words'], data['not_words'] is None):
c.Search = data['exact_phrase'] + " " + data['hashtag']
elif (data['exact_phrase'], data['not_words'] is not None) and (data['all_words'], data['any_words'], data['hashtag'] is None):
c.Search = data['exact_phrase'] + " " + data['not_words']
elif (data['not_words'], data['hashtag'] is not None) and (data['all_words'], data['any_words'], data['exact_phrase'] is None):
c.Search = data['not_words'] + " " + data['hashtag']
elif (data['all_words'], data['any_words'], data['exact_phrase'] is not None) and (data['hashtag'], data['not_words'] is None):
c.Search = data['all_words'] + " " + data['exact_phrase'] + " " + data['any_words']
elif (data['all_words'], data['any_words'], data['not_words'] is not None) and (data['hashtag'], data['exact_phrase'] is None):
c.Search = data['all_words'] + " " + data['any_words'] + " " + data['not_words']
elif (data['all_words'], data['any_words'], data['hashtag'] is not None) and (data['not_words'], data['exact_phrase'] is None):
c.Search = data['all_words'] + " " + data['any_words'] + " " + data['hashtag']
elif (data['all_words'], data['exact_phrase'], data['not_words'] is not None) and (data['any_words'], data['hashtag'] is None):
c.Search = data['all_words'] + " " + data['exact_phrase'] + " " + data['not_words']
elif (data['all_words'], data['exact_phrase'], data['hashtag'] is not None) and (data['any_words'], data['not_words'] is None):
c.Search = data['all_words'] + " " + data['exact_phrase'] + " " + data['hashtag']
elif (data['all_words'], data['not_words'], data['hashtag'] is not None) and (data['any_words'], data['not_words'] is None):
c.Search = data['all_words'] + " " + data['not_words'] + " " + data['hashtag']
elif (data['exact_phrase'], data['any_words'], data['not_words'] is not None) and (data['hashtag'], data['all_words'] is None):
c.Search = data['exact_phrase'] + " " + data['any_words'] + " " + data['not_words']
elif (data['exact_phrase'], data['any_words'], data['hashtag'] is not None) and (data['not_words'], data['all_words'] is None):
c.Search = data['exact_phrase'] + " " + data['any_words'] + " " + data['hashtag']
elif (data['any_words'], data['not_words'], data['hashtag'] is not None) and (data['all_words'], data['exact_phrase'] is None):
c.Search = data['any_words'] + " " + data['not_words'] + " " + data['hashtag']
elif (data['exact_phrase'], data['not_words'], data['hashtag'] is not None) and (data['all_words'], data['any_words'] is None):
c.Search = data['exact_phrase'] + " " + data['not_words'] + " " + data['hashtag']
elif (data['all_words'], data['exact_phrase'], data['any_words'], data['not_words'] is not None) and (data['hashtag'] is None):
c.Search = data['all_words'] + " " + data['exact_phrase'] + " " + data['any_words'] + " " + data['not_words']
elif (data['all_words'], data['exact_phrase'], data['any_words'], data['hashtag'] is not None) and (data['not_words'] is None):
c.Search = data['all_words'] + " " + data['exact_phrase'] + " " + data['any_words'] + " " + data['hashtag']
elif (data['all_words'], data['any_words'], data['not_words'], data['hashtag'] is not None) and (data['exact_phrase'] is None):
c.Search = data['all_words'] + " " + data['any_words'] + " " + data['not_words'] + " " + data['hashtag']
elif (data['all_words'], data['exact_phrase'], data['not_words'], data['hashtag'] is not None) and (data['any_words'] is None):
c.Search = data['all_words'] + " " + data['exact_phrase'] + " " + data['not_words'] + " " + data['hashtag']
elif (data['exact_phrase'], data['any_words'], data['not_words'], data['hashtag'] is not None) and (data['all_words'] is None):
c.Search = data['exact_phrase'] + " " + data['any_words'] + " " + data['not_words'] + " " + data['hashtag']
elif (data['all_words'], data['exact_phrase'], data['any_words'], data['not_words'], data['hashtag']) is not None:
c.Search = data['all_words'] + " " + data['exact_phrase'] + " " + data['any_words'] + " " + data['not_words'] + " " + data['hashtag']
【问题讨论】:
-
我很确定它们在效率方面几乎相同......(也许 if 语句以很小的优势获胜)......但它可能有助于使代码更具可读性
标签: python dictionary if-statement