将返回值直接附加到一组列表中 [英] Appending the return values to a set of list directly

查看:42
本文介绍了将返回值直接附加到一组列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表 list1 和 list2,我必须使用 func() 返回的值将结果附加到它们.

I have two list list1 and list2 and I have to append the result to both of them with the values returned by func().

item1,item2=func()
list1.append(item1)
list2.append(item2)

是否可以用单个语句替换上述内容?换句话说,如何在不创建任何临时变量的情况下将返回的元组附加到两个列表中?

Is it possible to replace the above by a single statement? In other words, how can I append the returned tuple to two lists without creating any temporary variables?

请比较损失的效率和原因.我无法理解.

Please compare the efficiency and reason in the loss as to why. I am unable to understand it.

推荐答案

您不想这样做,但是:

map(lambda l,v: l.append(v), (list1, list2), func())

在 Python3 中,您需要强制 map 进行迭代:

In Python3 you need to force the map to iterate:

list(map(lambda l,v: l.append(v), (list1, list2), func()))

这篇关于将返回值直接附加到一组列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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