【问题标题】:Efficient use of conditional statements over dictionary in python在python中有效地使用条件语句而不是字典
【发布时间】: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


【解决方案1】:

首先,你应该学会使用变量来减少代码中的重复。 另外,去看看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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-02
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多