python中字典上条件语句的高效使用

python中字典上条件语句的高效使用,python,dictionary,if-statement,Python,Dictionary,If Statement,我想检查这些值存在什么组合,一个条件是hashtag不能单独退出,所以组合是4C1+5C2+5C3+5C4+5C5,组成这些条件语句 我想知道是否有一种更有效的方法可以通过在python中使用字典方法来实现这一点 我的字典 data={'all_words':'','exact_phrase':'','any_words':'','not_words':'','hashtag':'} 我的条件语句 如果数据['all_words']不是无,并且(数据['any_words']、数据['exac

我想检查这些值存在什么组合,一个条件是hashtag不能单独退出,所以组合是4C1+5C2+5C3+5C4+5C5,组成这些条件语句

我想知道是否有一种更有效的方法可以通过在python中使用字典方法来实现这一点

我的字典

data={'all_words':'','exact_phrase':'','any_words':'','not_words':'','hashtag':'}
我的条件语句

如果数据['all_words']不是无,并且(数据['any_words']、数据['exact_phrase']、数据['not_words']、数据['hashtag']是无):
c、 搜索=数据[“所有单词”]
elif数据['any_words']不是None,并且(数据['all_words']、数据['exact_Phase']、数据['not_words']、数据['hashtag']是None):
c、 搜索=数据['任何单词']
elif数据['exact_Phase']不是无,并且(数据['all_words']、数据['any_words']、数据['not_words']、数据['hashtag']是无):
c、 搜索=数据[“精确短语”]
elif数据['hashtag']不是无,并且(数据['all_words']、数据['any_words']、数据['not_words']、数据['exact_Phase']是无):
c、 搜索=数据['hashtag']
elif(数据['all_words',数据['any_words']不是无)和(数据['exact_phrase',数据['not_words',数据['hashtag']是无):
c、 搜索=数据['all_words']+“”+数据['any_words']
elif(数据['all_words',数据['exact_phrase']不是无)和(数据['any_words',数据['not_words',数据['hashtag']是无):
c、 搜索=数据['all_words']+''+数据['exact_phrase']
elif(数据['all_words',数据['not_words']不是无)和(数据['any_words',数据['exact_phrase',数据['hashtag']是无):
c、 搜索=数据['all_words']+''+数据['not_words']
elif(数据['all_words',数据['hashtag']不是无)和(数据['any_words',数据['exact_phrase',数据['not_words']是无):
c、 搜索=数据['all_words']+''+数据['hashtag']
elif(数据['any_words',数据['exact_words']不是无)和(数据['all_words',数据['hashtag',数据['not_words']是无):
c、 搜索=数据['exact_Phase']+“”+数据['any_words']
elif(数据['any_words',数据['hashtag']不是无)和(数据['all_words',数据['exact_words',数据['not_words']是无):
c、 搜索=数据['any_words']+''+数据['hashtag']
elif(数据['any_words',数据['not_words']不是无)和(数据['all_words',数据['exact_Phase',数据['hashtag']是无):
c、 搜索=数据['any_words']+“”+数据['not_words']
elif(数据['exact_-Phase',数据['hashtag']不是无)和(数据['all_-words',数据['any_-words',数据['not_-words']是无):
c、 搜索=数据['exact_phrase']+''+数据['hashtag']
elif(数据['exact_-Phase',数据['not_-words']不是无)和(数据['all_-words',数据['any_-words',数据['hashtag']是无):
c、 搜索=数据['exact_Phase']+''+数据['not_words']
elif(数据['not_words',数据['hashtag']不是无)和(数据['all_words',数据['any_words',数据['exact_phrase']是无):
c、 搜索=数据['not_words']+''+数据['hashtag']
elif(数据['all_words',数据['any_words',数据['exact_phrase']不是无)和(数据['hashtag',数据['not_words']是无):
c、 搜索=数据['all_words']+''数据['exact_phrase']+''数据['any_words']
elif(数据['all_words',数据['any_words',数据['not_words']不是无)和(数据['hashtag',数据['exact_phrase']是无):
c、 搜索=数据['all_words']+''+数据['any_words']+''+数据['not_words']
elif(数据['all_words',数据['any_words',数据['hashtag']不是无)和(数据['not_words',数据['exact_phrase']是无):
c、 搜索=数据['all_words']+''+数据['any_words']+''+数据['hashtag']
elif(数据['all_words',数据['exact_phrase',数据['not_words']不是无)和(数据['any_words',数据['hashtag']是无):
c、 搜索=数据['all_words']+''数据['exact_phrase']+''数据['not_words']
elif(数据['all_words',数据['exact_phrase',数据['hashtag']不为无)和(数据['any_words',数据['not_words']不为无):
c、 搜索=数据['all_words']+''+数据['exact_短语']+''+数据['hashtag']
elif(数据['all_words',数据['not_words',数据['hashtag']不是无)和(数据['any_words',数据['not_words']是无):
c、 搜索=数据['all_words']+''+数据['not_words']+''+数据['hashtag']
elif(数据['exact_-Phase']、数据['any_-words']、数据['not_-words']不为无)和(数据['hashtag']、数据['all_-words']不为无):
c、 搜索=数据['exact_-Phase']+''数据['any_-words']+''数据['not_-words']
elif(数据['exact_-Phase']、数据['any_-words']、数据['hashtag']不为无)和(数据['not_-words']、数据['all_-words']不为无):
c、 搜索=数据['exact_-phrase']+''+数据['any_-words']+''+数据['hashtag']
elif(数据['any_words',数据['not_words',数据['hashtag']不是无)和(数据['all_words',数据['exact_phrase']是无):
c、 搜索=数据['any_words']+''+数据['not_words']+''+数据['hashtag']
elif(数据['exact_-Phase']、数据['not_-words']、数据['hashtag']不是无)和(数据['all_-words']、数据['any_-words']是无):
c、 搜索=数据['exact_-phrase']+''+数据['not_-words']+''+数据['hashtag']
elif(数据['all_words']、数据['exact_phrase']、数据['any_words']、数据['not_words']不为无)和(数据['hashtag']不为无):
c、 搜索=数据['all_-words']+''数据['exact_-phrase']+''数据['any_-words']+''数据['not_-words']
elif(数据['all_words']、数据['exact_phrase']、数据['any_words']、数据['hashtag']不是无)和(数据['not_words']是无):
c、 搜寻
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)