[Python 3.6.2] 5.1 Data Structure -- LIST METHODS

Python官网截取的data structure的用法
5.1 List
here are all the methods of list objects:
  • list.append()   # add an item to the end of the list.  equivalent to a[len(a):] = [x]
  • list.extend(iterable)    # extend the list by appending all the items from the iterable
[Notes]: the difference between append and extend!
list.append method appends an object to the end of the list. Whatever the object is (a string, another list, or sth else), it gets added onto the end of the original list as a single entry .
my_list = ['foo', 'bar']
my_list.append('baz')
my_list
>> ['foo','bar','baz']

another_list = [1,2,3]
my_list .append(another_list)
my_list
>>['foo','bar','baz',[1,2,3]]

The list.extend() method extends a list by appending elements from an iterable:
my_list.extend(anoher_list)
my_list
>>['foo','bar',1,2,3]
my_list.extend ('baz')
my_list
>>['foo','bar',1,2,3, 'b', 'a', 'z' ]
  • list.insert(i,x)    #insert an item at a given position. i is the index of the element before which to insert, x iss the item to be inserted.  
         a.insert(0, x) inserts at the front of the list
         a.insert(len(a), x) is equivalent to a.append(x)
  • list.remove(x)    # remove the first item from the list whose value is X. It is an error if there is no such item.
  • list.pop([i])    #remove the item at the given position in the list and return it.  if no index, a.pop() removes and returns the last item in the list. Note that: the squre brackets around i means: the parameter is optional  (此处.pop([i])中的中括号表示.pop()的参数是可选项,而不表示参数需要加上中括号,其他地方用法相同)
  • list.clear()    #remove all items from the list.Equivalent to del a[ : ]
  • list.index(x[, start[,end]])    # return 0-based index in the list of the first item whose value is x. Error raised if there is no such item. 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 compulated relative to the beginning of the full sequencce rather than the start argument.
  • list.count(x)    # return the number of times x appears in the list.
  • 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 elments of the list in place
  • list.copy()     #return a shallow copy of the list. Equivalent to a[ : ]

Example:
fruits = ['apple', 'banana', 'orange', 'watermelon', 'pear', 'banana','apple','orange','apple']
print(fruits.count('apple'))
>> 3

print(fruits.index('apple',3))
>>6

print(fruits)
>>['apple', 'banana', 'orange', 'watermelon', 'pear', 'banana', 'apple', 'orange', 'apple']

fruits.reverse()
print(fruits)
>>['apple', 'orange', 'apple', 'banana', 'pear', 'watermelon', 'orange', 'banana', 'apple']
fruits.sort()
print(fruits)
>>['apple', 'apple', 'apple', 'banana', 'banana', 'orange', 'orange', 'pear', 'watermelon']

print(fruits.pop(2))
>>apple

print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'orange', 'orange', 'pear', 'watermelon'] #前面.pop删除了位置2处的apple.

fruits.insert(4,'grape')
print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'grape', 'orange', 'orange', 'pear', 'watermelon']

vegetable = ['potato', 'tomato']
fruits.extend(vegetable)
print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'grape', 'orange', 'orange', 'pear', 'watermelon', 'potato', 'tomato']

fruits.append(vegetable)
print(fruits)
>>['apple', 'apple', 'banana', 'banana', 'grape', 'orange', 'orange', 'pear', 'watermelon', 'potato', 'tomato', ['potato', 'tomato']]

fruits.clear()
print(fruits)
>>[]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值