Python.比较2个列表中的数字并找到最大 [英] Python.Comparing numbers in 2 lists and finding max

查看:582
本文介绍了Python.比较2个列表中的数字并找到最大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个元素类似的列表:

I have 2 lists with elements like:

list1=[2,54,31,6,42]
list2=[4,98,43,3,2]

我想要一个可以比较数字的def,并返回包含最大数字的第三个列表.

I want a def that compares the numbers and returns a 3rd list with the biggest one.

在此示例中,第三个列表为:

In this example the 3rd list would be:

list3=[4,98,43,6,42]

推荐答案

下面是一个简单的def/函数,用于zip()两个列表,然后获取max()并将其存储到新的list3中并返回:

Here's a simple def/function to zip() the two lists and then get max() and store it into a new list3 and returned:

list1=[2,54,31,6,42]
list2=[4,98,43,3,2]

def function(list1,list2): #def returns 3rd list 
    list3 = [max(value) for value in zip(list1, list2)]
    return list3
print(function(list1,list2)) # call def named function to print

输出:

[4, 98, 43, 6, 42]

这篇关于Python.比较2个列表中的数字并找到最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆