李二蛋 发表于 2022-2-25 16:32:54

如何对嵌套列表进行增删改

我想对x进行切片语法,但看起来貌似不行,请各位前辈指点
>>> x
[, , ]
>>> x=1
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
    x=1
TypeError: can only assign an iterable
>>> x=1
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
    x=1
TypeError: can only assign an iterable
>>> x=1
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
    x=1
TypeError: can only assign an iterable

isdkz 发表于 2022-2-25 16:40:33

本帖最后由 isdkz 于 2022-2-25 22:19 编辑

可以切片,但是切片不能赋值为整数,

对切片赋值只能是可迭代对象,你可以赋值为列表

wp231957 发表于 2022-2-25 17:17:48

使用索引,不要切片
x=1

大马强 发表于 2022-2-25 17:21:48

你这样对一个不是嵌套的列表也不行啊
>>> y

>>> y = 1
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
    y = 1
TypeError: can only assign an iterable


>>> x
[, ]
>>> x.append()
>>> x
[, , ]


>>> del x
>>> x
[, ]


>>> x =
>>> x
[, ]
>>> x=
>>> x
[, ]

傻眼貓咪 发表于 2022-2-25 22:48:43

arr = [, , ]

print(arr)
print(arr[::-1])
print(arr[::2])
print(arr[-1:0:-1])


python爱好者. 发表于 2022-2-26 05:04:49

使用切片就不是只一个位置,要穿进去多个值,而你就只传了个 1 当然报错!

李二蛋 发表于 2022-2-26 14:52:40

isdkz 发表于 2022-2-25 16:40
可以切片,但是切片不能赋值为整数,

对切片赋值只能是可迭代对象,你可以赋值为列表

您说到点上了,谢谢您

李二蛋 发表于 2022-2-26 14:54:43

傻眼貓咪 发表于 2022-2-25 22:48


您切片玩得太六了!感谢您的指点{:10_298:}

李二蛋 发表于 2022-2-26 14:55:15

python爱好者. 发表于 2022-2-26 05:04
使用切片就不是只一个位置,要穿进去多个值,而你就只传了个 1 当然报错!

是的,要可迭代对象,谢谢您{:10_298:}
页: [1]
查看完整版本: 如何对嵌套列表进行增删改