Python:列表list方法 ①list.append() ②list.count() ③list.extend() ④list.index() ⑤list.insert(index, obj)

①list.append()
在列表末尾添加元素

aList = [123, 'xyz', 'zara', 'abc'];
aList.append( 2009 );
print "Updated List : ", aList;

结果

Updated List :  [123, 'xyz', 'zara', 'abc', 2009]

②list.count()
统计目的元素在列表中出现的次数

aList = [123, 'xyz', 'zara', 'abc', 123];
print "Count for 123 : ", aList.count(123);
print "Count for zara : ", aList.count('zara');

结果

Count for 123 :  2
Count for zara :  1

③list.extend()
在列表末尾一次性添加另一个列表的所有元素(列表扩展)

aList = [123, 'xyz', 'zara', 'abc', 123];
bList = [2009, 'manni'];
aList.extend(bList)

print "Extended List : ", aList ;

结果

Extended List :  [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']

④list.index()
对元素在列表中进行索引,返回元素在列表的位置

aList = [123, 'xyz', 'zara', 'abc'];

print "Index for xyz : ", aList.index( 'xyz' ) ;
print "Index for zara : ", aList.index( 'zara' ) ;

结果

Index for xyz :  1
Index for zara :  2

⑤list.insert(index, obj)
插入元素到列表中

aList = [123, 'xyz', 'zara', 'abc']

aList.insert( 3, 2009)

print "Final List : ", aList

结果

Final List : [123, 'xyz', 'zara', 2009, 'abc']

引用:www.runoob.com/python/python-lists.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值