我想检查这些值存在什么组合,一个条件是#号不能单独退出,因此组合是构成这些条件语句的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']
最佳答案
首先,您应该学习使用变量来减少代码中的重复。
另外,请参阅python dict python dict的基本知识
我尝试重写您的代码,告诉您是否要执行该程序
def not_none_dict(d):
new_d = {}
for k, v in d.items():
if d[k] is not None or d[k] == '':
new_d[k] = d[k]
return new_d
def format_dict(d):
all_words = new_data.get('all_words', '')
exact_phrase = new_data.get('exact_phrase', '')
any_words = new_data.get('any_words', '')
not_words = new_data.get('not_words', '')
hashtag = new_data.get('hashtag', '')
Search = '{}{}{}{}{}'.format(all_words, exact_phrase, any_words, not_words, hashtag)
print(Search)
Search = ' '.join(Search)
return Search
data = {'all_words': '1', 'exact_phrase': '2', 'any_words': None, 'not_words': '4', 'hashtag': None}
new_data = not_none_dict(data)
print(new_data)
if len(new_data) == 0 or new_data is None:
Search = 'error'
elif len(new_data) == 1:
Search = list(new_data.values())[0]
elif len(new_data) == len(data):
Search = format_dict(new_data)
else:
Search = format_dict(new_data)
print (Search)