python一些常用方法_Python的一些常用列表方法

5.1. More on Lists关于列表的知识

The list data type has some more methods. Here are all of the methods of list objects:除了第前面的,列表还有更多的方法,下面就是列表对象的所有方法:

list.append(x)

Add an item to the end of the list. Equivalent to a[len(a):] = [x].本方法是添加一个元素到列表末,相当于a[len(a):] = [x]

list.extend(iterable)

Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.从iterable中追加所有项来扩展列表。等效a[len(a):] = iterable。

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).本方法是在指定的位置插入一个对象,第一个参数是要插入元素的位置,a.insert(0, x)是在当前列表的前面插入,a.insert(len(a),x)等效于a.append(x)。

list.remove(x)

Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.从列表中移出值为x的第一个对象。如果找不到相应的对象会提供一个错误代码ValueError。

list.pop([i])

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)从列表中移出给定位置的对象,如果没有给定索引号这移出最后一个对象并返回列表中最后的对象(参数i用方括号括起来表示该参数是可选的,您将经常在 Python 库引用中看到此符号)。

list.clear()

Remove all items from the list. Equivalent to del a[:].移除列表中所有的对象。等效于del a[:]。

list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.在等于 x 的第一个项中返回从零开始的索引. 如果没有此类项目, 则引发 ValueError错误。

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.可选参数的开始和结束解释为切片表示法, 用于将搜索限制为列表的特定子序列。返回的索引是相对于完整序列的开始而不是开始参数计算的。

list.count(x)

Return the number of times x appears in the list.返回x在列表中出现的次数。

list.sort(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.反向排列列表中的对象。

list.copy()

Return a shallow copy of the list. Equivalent to a[:].返回一个列表的镜像,等效于a[:]。

An example that uses most of the list methods:下面是这些方法的实例:

>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']

>>> fruits.count('apple')

2

>>> fruits.count('tangerine')

0

>>> fruits.index('banana')

3

>>> fruits.index('banana', 4) # Find next banana starting a position 4

6

>>> fruits.reverse()

>>> fruits

['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']

>>> fruits.append('grape')

>>> fruits

['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']

>>> fruits.sort()

>>> fruits

['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']

>>> fruits.pop()

'pear'

ed679901e680eb5ae25c405eb3996eb0.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值