简体   繁体   中英

Finding even numbers in lists of lists python

I want to how to find even numbers from lists of lists.
The example was:

maximum_even_each_list_in_lol([[1,2,3],[6,5,4],[5,7,9]])

should return (maximum even numbers):

[2,6,0]

I found how to find even numbers from list, from stackoverflow:

def maximum_even_each_list_in_lol (lol):
    evens=[]
    for number in lol:
        if is_even(number):
            evens.append(number)
    return evens

but I want to know how I can make it so that it can find even numbers from each list, in list.

Though you don't say it explicitly, it seems you are looking for the maximum even number in each list within a list of lists. And from your example it seems you want the programme to add 0 if there is no even number. In that case this will work:

print [max(value for value in [0] + L if not value % 2) for L in list_of_lists]

You can build this algorithm up from the inside-out using some nice Pythonic language constructs:

# Function to determine if one number is even
is_even = lambda x : x%2==0

# Function to return just the even numbers of a list
just_evens = lambda x : filter(is_even, x)

# Function to return the maximum number in a list, or '0' if there is none
get_max = lambda x : max(x) if len(x) else 0

# Function to return the maximum even number of a list
max_even = lambda x : get_max(just_evens(x))

# Print out the maximum even number from each list:
map(max_even, [[1,2,3],[6,5,4],[5,7,9]])

This will print out:

[2, 6, None]

python 3.4 takes a default value for max so if after filtering there are no even numbers we can return 0 as a default value:

l = [[1,2,3],[6,5,4],[5,7,9]]
print([max(filter(lambda x: not x % 2,x),default=0) for x in l])
[2, 6, 0]

You're almost there! :-)

Check what the builtin max function does.

def maximum_even_each_list_in_lol (lol):
    evens=[0]  # Dirty trick to ensure that regardless of the `even` numbers
               # found, you'll always have a zero among the results.
    for number in lol:
        if is_even(number):
            evens.append(number)
    print "I found these even numbers (%s) among %s" % (evens, lol)
    return max(evens)  # Return the maximum value among the `evens` list.
results = []
for lst in [[1,2,3],[6,5,4],[5,7,9]]:
    print "Studying %s" % lst
    results.append(maximum_even_each_list_in_lol(lst))
print "This is what I found: %s" % results

I added some print statements to help follow what the code is doing. The code above will output:

Studying [1, 2, 3]
I found these even numbers ([0, 2]) among [1, 2, 3]
Studying [6, 5, 4]
I found these even numbers ([0, 6, 4]) among [6, 5, 4]
Studying [5, 7, 9]
I found these even numbers ([0]) among [5, 7, 9]
This is what I found: [2, 6, 0]

As I understand you want to iterate over a list of lists , grabbing biggest even number from each list or 0 if not even numbers were found. Here is your function then:

def maximum_even_each_list_in_lol(lol):
    evens = []
    for sublist in lol:
        subevens = []
        for number in sublist:
            if (not number % 2):
                subevens.append(number)
        # As I understand you want to insert 0
        # if no even numbers were found, right?
        if not subevens:
            subevens = [0]
        evens.append(max(subevens))
    return evens

# Testing
assert maximum_even_each_list_in_lol([[1,2,3],[6,5,4],[5,7,9]]) == [2,6,0]

Make sure to read about builtin max function :)

here you go:

>>> [ max(x) if len(x)>0 else 0 for x in [ list(filter(lambda x:x%2==0,x)) for x in a ]]
[2, 6, 0]

i used lambda to check for even number, then filter to save value for true, then max if lenght of list greater then 0 else 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM