【问题标题】:How do I return this as a list如何将此作为列表返回
【发布时间】:2011-02-04 02:27:51
【问题描述】:

我有这些数据:

inventory = { HAMMER: (10,100),
              SCREW: (1, 1000),
              NAIL: (1, 1000),
              SCREWDRIVER: (8, 100),
              DRILL: (50, 20),
              WORKBENCH: (150, 5),
              HANDSAW: (15, 50),
              CHAINSAW: (80, 30)
            }

我写了以下函数:

def get_items(cheapness):
    """ Return a list of (item, (price, count)) tuples that are the given
    cheapness. Note that the second element of the tuple is another tuple. """

    if cheapness == 'CHEAP':
        return [(key, value) for (key,value) in inventory.items() if value in ((cost,quantity) for (cost, quantity) in inventory.values() if cost < 20)]
    elif cheapness == 'MODERATE':
        return [(key, value) for (key,value) in inventory.items() if value in ((cost,quantity) for (cost, quantity) in inventory.values() if 20 < cost < 100)]
    elif cheapness == 'EXPENSIVE':
        return [(key, value) for (key,value) in inventory.items() if value in ((cost,quantity) for (cost, quantity) in inventory.values() if cost > 100)]

我的问题是当我执行print type(get_items(CHEAP)) 时,我得到&lt;type 'NoneType'&gt;。如何将结果作为列表返回?

【问题讨论】:

    标签: python dictionary nested-lists


    【解决方案1】:

    此代码功能更强大,可能更易于维护:

    def get_items(cheapness):
        def a(value):
            if cheapness == 'CHEAP':
                return value < 20
            if cheapness == 'MODERATE':
                return value >= 20 and value < 100
            if cheapness == 'EXPENSIVE':
                return value >= 100
            return False
        return filter(lambda x: a(x[1][0]), inventory.items())
    

    【讨论】:

    • 恐怕我还没有真正使用 lambda。不过,我非常喜欢 Russell Borogove 的回答。请问你的 Pythonic 怎么样?
    • 当我看到同一行代码重复多次时,我总是问自己如何才能做得更好。在您的情况下,您正在写出相同的列表理解三遍。唯一的区别是条件语句。我的将条件语句提取到一个函数中,并利用 Python 中内置的函数工具(即:filterlambda)。
    • 我自己不一定会说更多 Pythonic;它更像是函数式编程风格。当过滤条件变得更复杂时,它的扩展性比我的解决方案好一点。
    • 很公平。从长远来看,这段代码也更容易维护。假设您必须再添加三个cheapness 级别。在您的代码中,这意味着复制/粘贴列表理解并修改条件。现在当库存字典发生变化时会发生什么(比如你向元组添加评级值)?您现在有六行需要更改。有了我的代码,你就有了。 PS:编辑了我的答案,指出它比 Pythonic 更实用。
    • @RJ Regenold 说得好。谢谢。但我会接受罗素的,因为这是我目前理解的解决方案,因为我是菜鸟。
    【解决方案2】:

    说出你的意思:

    def get_items(cheapness):
        """ Return a list of (item, (price, count)) tuples that are the given
        cheapness. Note that the second element of the tuple is another tuple. """
    
        if cheapness == 'CHEAP':
            return [(item, (price,count)) for (item, (price,count)) in inventory.items() if price < 20]
        elif cheapness == 'MODERATE':
            return [(item, (price,count)) for (item, (price,count)) in inventory.items() if price > 20 and price < 100]
        elif cheapness == 'EXPENSIVE':
            return [(item, (price,count)) for (item, (price,count)) in inventory.items() if price > 100]
    

    【讨论】:

    • 但由于他的菜鸟,这是 OP 在此时理解的解决方案。保持优雅。
    【解决方案3】:

    打印类型(get_items('CHEAP')) 怎么样? (注意引号)

    【讨论】:

    • 实际上,我认为这是您的直接问题。你所有的情况最坏的情况应该是返回 [],而不是 None;您正在从 get_items() 的末尾跌落而没有返回。但我的回答仍然更清晰、更有效(他谦虚地说)。
    猜你喜欢
    • 2021-01-27
    • 2015-05-23
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多