python List 用法

本文对比了Python中的列表和数组,介绍了列表的各种方法如append、insert等,并讨论了它们之间的主要区别,即列表更为灵活但可能浪费空间,而数组则假设所有元素类型相同从而节省空间。

列表:非常的灵活,可以存储任意的数据类型,还支持append()方法,问题是浪费大量的空间

数组: 假设你知道 array 中所有的数据类型都是一样的话,那么你就可以在空间上节省一大半。

list 各种用法:

list.append('d'#will add 'd' to the list
list[
0#will get the first item 'a'

list.insert(i, x) 
# Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).    

list.pop(
2# will remove items by postion, remove the 2nd item
list.remove(x) 
# Remove the first item from the list whose value is x.

list.index(x) 
# Return the index in the list of the first item whose value is x. It is an error if there is no such item.

list.count(x) 
# Return the number of times x appears in the list.

list.sort(cmp=
None, key=None, reverse=False# Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).

list.reverse() 
# Reverse the elements of the list, in place.


转载于:https://my.oschina.net/pythonnnn/blog/660710

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值