python输出列表中最大的数是多少,Python。比较2个列表中的数字并找到最大

I have 2 lists with elements like:

list1=[2,54,31,6,42]

list2=[4,98,43,3,2]

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]

解决方案

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

Output:

[4, 98, 43, 6, 42]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值