【发布时间】:2021-07-12 00:37:00
【问题描述】:
我有一个这样的列表[1, 2, 3, [4, 5, 6]]
如何删除列表中的[ 字符,以便获得[1, 2, 3, 4, 5, 6]?
这是我目前的代码:
a = [1, 2, 3, [4, 5, 6]]
new_a = []
for item in a:
if len(item) > 1:
for sub_item in item:
new_a.append(sub_item)
else:
new_a.append(item)
print(new_a)
然后我得到了这个错误:
TypeError: object of type 'int' has no len()
但是当我用len(a[3]) 得到内部列表的长度时,它返回3。
我该如何解决这个问题?
【问题讨论】:
-
@AlwaysSunny 感谢您的评论,多么棒的帖子。