[Python 3.6.2] 5.1 Data Structure -- LIST METHODS

来源:互联网 发布:JavaScript post 编辑:程序博客网 时间:2024/06/14 21:15
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 todel 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, seesorted 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)
>>[]

原创粉丝点击