Python 将两个返回附加到两个不同的列表 [英] Python appending two returns to two different lists

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

问题描述

我想将两个返回的列表附加到两个不同的列表中,例如

I am wanting to append two returned lists to two different lists such as

def func():
    return [1, 2, 3], [4, 5, 6]

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

有什么想法吗?

推荐答案

您必须先捕获返回值,然后 append:

You'll have to capture the return values first, then append:

res1, res2 = func()
list1.append(res1)
list2.append(res2)

您似乎在此处返回列表,您确定不是要改用 list.extend() 吗?

You appear to be returning lists here, are you certain you don't mean to use list.extend() instead?

如果您要扩展 list1list2,则可以使用切片分配:

If you were extending list1 and list2, you could use slice assignments:

list1[len(list1):], list2[len(list2):] = func()

但这对新人来说是 a) 令人惊讶和 b) 在我看来相当不可读.我仍然会使用单独的分配,然后扩展调用:

but this is a) surprising to newcomers and b) rather unreadable in my opinion. I'd still use the separate assignment, then extend calls:

res1, res2 = func()
list1.extend(res1)
list2.extend(res2)

这篇关于Python 将两个返回附加到两个不同的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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