Python如何通过lambda和map配对两个列表 [英] Python How to pair two list by lambda and map

查看:352
本文介绍了Python如何通过lambda和map配对两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有以下两个列表

For example, I have following two lists

listA = ['一个','两个','三个'] listB = ['苹果','樱桃','西瓜']

listA=['one', 'two' , 'three'] listB=['apple','cherry','watermelon']

如何使用maplambda将这两个列表配对以获得此输出?

How can I pair those two lists to get this output, using map and lambda?

one apple
two cherry
three watermelon

我知道如何通过列表理解来做到这一点,

I know how to do it by the list comprehension,

[print(listA[i], listB[i]) for i in range(len(listA))]

但是我不知道maplambda解决方案.有什么想法吗?

but I can't figure out a map and lambda solution. Any ideas?

推荐答案

这是我根据您所需要的内容(地图和lambda)

Here what I got based on what you need (map and lambda),

输入:

listA=['one', 'two' , 'three']
listB=['apple','cherry','watermelon']
list(map(lambda x, y: x+ ' ' +y, listA, listB))

输出:

['one apple', 'two cherry', 'three watermelon']

这篇关于Python如何通过lambda和map配对两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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