列表操作

如何更新列表
你可以通过在等号的左边指定一个索引或者索引范围的方式来更新一个或几个元素,你也
可以用 append()方法来追加元素到列表中去.

aList
[123, ‘abc’, 4.56, [‘inner’, ‘list’], (7-9j)]
aList[2]
4.56
aList[2] = ‘float replacer’
aList
[123, ‘abc’, ‘float replacer’, [‘inner’, ‘list’], (7-9j)]

anotherList.append(“hi, i’m new here”)
Edit By Vheavens
Edit By Vheavens                              
print anotherList
[None, ‘something to see here’, “hi, i’m new here”]
aListThatStartedEmpty.append(‘not empty anymore’)
print aListThatStartedEmpty
[‘not empty anymore’]
如何删除列表中的元素或者列表(本身)
要删除列表中的元素,如果你确切的知道要删除元素的素引可以用 del 语句,否则可以用
remove()方法.
aList
[123, ‘abc’, ‘float replacer’, [‘inner’, ‘list’], (7-9j)]
del aList[1]
aList
[123, ‘float replacer’, [‘inner’, ‘list’], (7-9j)]
aList.remove(123)
aList
[‘float replacer’, [‘inner’, ‘list’], (7-9j)]
你还可以通过 pop()方法来删除并从列表中返回一个特定对象.
一般来说,程序员不需要去删除一个列表对象。列表对象出了作用域(比如程序结束,函数调
用完成等等)后它会自动被析构,但是如果你想明确的删除一整个列表,你可以用 del 语句:
del aList

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值