首页 > 代码库 > python 数据类型---列表使用 之二 (增删改查)

python 数据类型---列表使用 之二 (增删改查)

列表的操作

1.列表的修改

>>> name
[Frank, Lee, 2, [Andy, Troy]]
>>> name[0] = "Frank Bian"
>>> name
[Frank Bian, Lee, 2, [Andy, Troy]]

2.列表的插入

>>> name
[Frank Bian, Lee, 2, [Andy, Troy]]
>>> name.insert(1,"Marlon")  #插入到位置就是其下标的值
>>> name
[Frank Bian, Marlon, Lee, 2, [Andy, Troy]]

3 列表的追加(append object to end)

>>> name
[Frank Bian, Marlon, Lee, 2, [Andy, Troy]]
>>> help(name.append)

>>> help(name.append)

>>> name.append(Duff)
>>> name
[Frank Bian, Marlon, Lee, 2, [Andy, Troy], Duff]

4 列表元素删除(L.remove(value) -> None -- remove first occurrence of value.
    Raises ValueError if the value is not present.)

>>> name.append(Duff)
>>> name
[Frank Bian, Marlon, Lee, 2, [Andy, Troy], Duff, Duff]
>>> name.remove(Duff)
>>> name
[Frank Bian, Marlon, Lee, 2, [Andy, Troy], Duff]

 

python 数据类型---列表使用 之二 (增删改查)