blackened
blackened

Reputation: 903

Python function sort and manipulate based on Arguments

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) For the function some_generator, how do I achieve: if sum_limit is None, then change the if condition

if sum(i) <= sum_limit or i[0] < index_value:

to

if i[0] < index_value:

or vice versa (if index_value is None, then change the if condition to:

if sum(i) < sum_limit:

2) For the function foo, how do I sort based on an argument? (For example, I may wish to sort the list based on last element's value, or I may wish to sort based on each elements sum.)

Upvotes: 1

Views: 158

Answers (2)

Kasravnd
Kasravnd

Reputation: 107287

You can use a logical and in your condition and for sorting you can use key argument in sorted function :

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)

Note that since in python 2.X you can compare the difference type objects like None and integer since every integer is larger than None and since sum_limit and index_vallue are supposed to be grater than your items if they be None it will be evaluated as False, thus the condition will be depends on other part, and you don't need to check the validity if sum_limit and index_value.

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

Upvotes: 3

tglaria
tglaria

Reputation: 5866

1) You could just use

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

if sum_limit == None, then the evaluation sum(i) <= sum_limit is skipped. Same thing with index_value

Example:

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

It will print 'None' and no error will raise if xyz doesn't exist.

2) Just read the manual. Add the 'key' variable to your sorted function. google python + sort

Upvotes: 2

Related Questions