【问题标题】:Python function sort and manipulate based on Arguments基于参数的 Python 函数排序和操作
【发布时间】:2016-03-16 05:04:51
【问题描述】:
def some_generator(sum_limit, index_value):
    """
    >>> list(some_generator(20, 4))
    [[1, 2, 3], [2, 5, 6], [3, 7, 10], [4, 5, 11], [3, 5, 15], [4, 5, 9]]

    """
    a = [[1, 2, 3], [2, 5, 6], [3, 7, 10], [4, 5, 11],
         [3, 5, 15], [4, 5, 9], [4, 6, 12], [1, 5, 19]]
    for i in a:
        if sum(i) <= sum_limit or i[0] < index_value:
            yield i
        else:
            break


def foo(_generator):
    """
    >>> foo(some_generator(20, 4))
    [[1, 2, 3], [2, 5, 6], [3, 5, 15], [3, 7, 10], [4, 5, 9], [4, 5, 11]]
    """
    x = list(_generator)
    return sorted(x)

1) 对于函数some_generator,如何实现:如果sum_limitNone,则改变if条件

if sum(i) &lt;= sum_limit or i[0] &lt; index_value:

if i[0] &lt; index_value:

反之亦然(如果index_valueNone,则将if条件改为:

if sum(i) &lt; sum_limit:

2) 对于函数foo,如何根据参数进行排序? (例如,我可能希望根据最后一个元素的值对列表进行排序,或者我可能希望根据每个元素的总和进行排序。)

【问题讨论】:

  • 每个帖子一个问题,请。

标签: python sorting python-3.x arguments


【解决方案1】:

1) 你可以使用

if ((sum_limit is not None) and sum(i) <= sum_limit) or ((index_value is not None)) and i[0] < index_value):

如果sum_limit == None,则跳过评估sum(i) &lt;= sum_limit。 与index_value 相同

例子:

print (None and xyz) or (True or xyz)

如果 xyz 不存在,它将打印“无”并且不会引发错误。

2) 只需阅读手册。将 'key' 变量添加到您的 sorted 函数中。谷歌 python + 排序

【讨论】:

  • x = list(_generator)拉数据吗?
  • 只使用一个对象的布尔值的问题是一些对象将被认为是假的,尽管是有效值(例如零)。你应该更明确地使用sum_limit is not None and sum(i) &lt;= sum_limit
【解决方案2】:

您可以在您的条件中使用逻辑and,并且可以在sorted 函数中使用key 参数进行排序:

def some_generator(iterator ,sum_limit, index_value):
    """
    >>> list(some_generator(20, 4))
    [[1, 2, 3], [2, 5, 6], [3, 7, 10], [4, 5, 11], [3, 5, 15], [4, 5, 9]]

    """
    for i in iterator:
        if (sum_limit and (sum(i) <= sum_limit)) or  (index_value and (i[0] < index_value)):
            yield i
        else:
            break


def foo(_generator, custom_key = None):
    """
    >>> foo(some_generator(20, 4))
    [[1, 2, 3], [2, 5, 6], [3, 5, 15], [3, 7, 10], [4, 5, 9], [4, 5, 11]]
    """
    x = list(_generator)
    return sorted(x,key= custom_key)

iterator = [[1, 2, 3], [2, 5, 6], [3, 7, 10], [4, 5, 11],
         [3, 5, 15], [4, 5, 9], [4, 6, 12], [1, 5, 19]]

print foo(some_generator(iterator,20, 4), custom_key=sum)

请注意,由于在 python 2.X 中,您可以比较 Noneinteger 之类的差异类型对象,因为每个整数都大于 None 并且因为 sum_limitindex_vallue 应该比您的项目更大如果它们是 None 它将被评估为 False,因此条件将取决于其他部分,并且您不需要检查 sum_limitindex_value 的有效性。

>>> 0>None
True
>>> 4>None
True
>>> -4>None
True
>>> 4<None
False
>>> 0<None
False
>>> -2<None
False

【讨论】:

  • @blackened 您可以传递some_generator 中的键索引,并在每次迭代中根据该索引对i 进行排序。
  • 第一部分没看懂,用and有什么问题?
  • @Kasramvd 也许我错过了一些东西。每个元素也是一个列表。如何包含根据内部列表总和(或产品)对列表的最终列表进行排序的选项,这将由(可选)参数传递?
  • @tglaria 没问题,它也是正确的。但是在这里是多余的。
  • @blackened 因此,而不是索引传递键作为函数。对于基于嵌套列表总和的排序,您可以传递 sum 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
相关资源
最近更新 更多