蟒追加两回,两个不同的表 [英] Python appending two returns to two different lists

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

问题描述

我想追加两人回到列表,以两个不同的列表,如

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()

任何想法?

推荐答案

您必须先获取返回值,,然后追加:

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?

如果你延长 list1的列表2 ,您可以用切片赋值:

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

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

但是这是一个惊人的),以新人和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)

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

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