简体   繁体   English

如何列出列表列表中的最大值(python)

[英]How to make a list of the maximums in a list of lists (python)

Out of all the little lists, I have to make one list with the maximum even number out of each of the lists. 在所有小列表中,我必须使用每个列表中的最大偶数制作一个列表。 If the list has odd numbers then the maximum is 0. So far my code is: 如果列表有奇数,则最大值为0.到目前为止,我的代码是:

a=[]
b=[]
c=''
d=''
e=''
for i in range (len(res)):
    for j in range (len(res)):
        d=res[i][j]
        if (d%2 == 0):
            d=d
            a.append(d)
        else:
            d= 0
            a.append(d)
    c = max(a)
    b.append(c)
print b

The list is [[1,2,3],[6,5,4],[5,7,9]] and I need to get [2,6,0] but instead I keep getting [2,6,6] 列表是[[1,2,3],[6,5,4],[5,7,9]] ,我需要得到[2,6,0]但我不断得到[2,6,6]

a = [[1,2,3],[6,5,4],[5,7,9]]
[ max( filter(lambda y: y % 2 ==0,(i+[0]))) for i in a]
>>>[2, 6, 0]

I think the code is quite self explanatory: 我认为代码非常自我解释:

  • max(seq) return a biggest number in the sequence max(seq)返回序列中的最大数字
  • filter(fn, seq) , will apply each element in seq with function fn, and keep element with fn(element) is logical true. filter(fn, seq)将使用函数fn应用seq中的每个元素,并且使用fn(element)保持元素是逻辑真。
  • list + [0] will be return a new list with one more element "0",just in case all elements in the list is odd number . list + [0]将返回一个包含一个元素“0”的新列表,以防列表中的所有元素都是奇数。

Update: in case of neg even, (credit should be give to @twasbrillig ) 更新:如果是偶数,(信用应该给@twasbrillig)

[ 0 if all(map(lambda z: z%2,i)) == True else max( filter(lambda y: y % 2 ==0,i)) for i in a]

max takes an optional key function which can be used to keep odd numbers out of the running. max采用可选的key功能,可用于保持奇数不在运行中。 See https://docs.python.org/library/functions.html#max 请参阅https://docs.python.org/library/functions.html#max

def fn(x):
    return x if x % 2 == 0 else float("-inf")

a = [[1,2,3],[6,5,4],[5,7,9]]
b = [ max(L, key=fn) for L in a ]
print([i if i % 2 == 0 else 0 for i in b])

gives [2, 6, 0] 给出[2, 6, 0]

Works with negative numbers too. 也可以使用负数。 (If you don't need to support negative numbers, you can replace float("-inf") with -1 .) (如果您不需要支持负数,则可以用-1替换float("-inf") 。)

As max function accepts an iterable, you can just use: 由于max函数接受iterable,您可以使用:

>>> a = [[1,2,3],[6,5,4],[5,7,9]]
>>> [max([m for m in i if m % 2 == 0] or [0]) for i in a]
[2, 6, 0]

Where [m for m in i if m % 2 == 0] gets the list of even numbers, and max([m for m in i if m % 2 == 0] or [0]) gets the max even number (negative included) or 0 if no even number exists. 其中[m for m in i if m % 2 == 0]得到偶数列表, max([m for m in i if m % 2 == 0] or [0])得到最大偶数(如果不存在偶数,则为0或0。

>>> a = [[1,2,3],[6,5,4],[5,7,9,-2]]
>>> [max([m for m in i if m % 2 == 0] or [0]) for i in a]
[2, 6, -2]

Several points. 几点。

One, replace range with xrange. 一,用xrange替换范围。 It doesn't create the full list. 它不会创建完整列表。

Second, it appears that you are iterating over a 'matrix'. 其次,看起来你正在迭代'矩阵'。 I had assumed that all the little lists were of indeterminate size. 我原以为所有的小名单都是不确定的。 Nevertheless you are performing n*n evaluations. 不过,您正在进行n * n评估。 You could instead perform n+n by first finding the max then checking for evenness or oddness. 您可以通过首先找到最大值然后检查均匀度或奇数来执行n + n。

maxes = [ max( row ) for row in res ]
res_final = [ m if m % 2 == 0 else 0 for m in maxes ]

You'll have to double check the last line. 你必须仔细检查最后一行。 But that should do. 但那应该做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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