串联Python列表时出现问题 [英] Problem concatenating Python list

查看:82
本文介绍了串联Python列表时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过执行以下操作来连接两个列表,一个仅包含一个元素:

I am trying to concatenate two lists, one with just one element, by doing this:

print([6].append([1,1,0,0,0]))

但是,Python返回None.我在做什么错了?

However, Python returns None. What am I doing wrong?

推荐答案

使用+运算符

>>> [6] + [1,1,0,0,0]
[6, 1, 1, 0, 0, 0]

您尝试执行的操作是将一个列表追加到另一个列表中,这将导致

What you were attempting to do, is append a list onto another list, which would result in

>>> [6].append([1,1,0,0,0])
[6, [1,1,0,0,0]]

为什么看到None返回,是因为.append具有破坏性,修改了原始列表并返回了None.它不会返回您要附加的列表.因此,您的列表 已被修改,但是您正在打印函数.append的输出.

Why you are seeing None returned, is because .append is destructive, modifying the original list, and returning None. It does not return the list that you're appending to. So your list is being modified, but you're printing the output of the function .append.

这篇关于串联Python列表时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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