Python list operating skills
- 2020-05-27 06:31:21
- OfStack
I have been learning python recently. Learned the basic grammar of python. I feel that python's position in data processing is inseparable from its list operations.
Learn the basics and take notes here.
'''
Python --version Python 2.7.11
Quote : https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
Add by camel97 2017-04
'''
list.append(x) # Add at the end of the list 1 Three new elements
Add an item to the end of the list; equivalent to a[len(a):] = [x].
list.extend (L)# merges two elements from list into 1
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
list.insert (i, x)# inserts the element into the specified position (the position is the first element indexed with i)
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.remove (x)# delete the element in list where the first value is x (that is, if there are two x in list, only the first x will be deleted)
Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop ([i]])# removes the i element in list and returns this element. If the parameter i is not given, the last element in list is deleted by default
Remove the item the given in the list, and return it If index is specified, a.pop () removes and the last in in in the the method signature denote that parameter is optional not that you should type square brackets at that position You see notation frequently in Python Library Reference.)
list.index (x)# returns the index of the element in list with the value X
Return the index in the of first item is x It no no such item
list.count (x)# returns the number of elements in list whose value is x
Return the number of times x appears in the list.
demo:
#-*-coding:utf-8-*-
L = [1,2,3] # create list
L2 = [4,5,6]
print L
L.append(6) # add
print L
L.extend(L2) # merge
print L
L.insert(0,0) # insert
print L
L.remove(6) # delete
print L
L.pop() # delete
print L
print L.index(2)# The index
print L.count(2)# count
L.reverse() # Reverse order
print L
result:
[1, 2, 3]
[1, 2, 3, 6]
[1, 2, 3, 6, 4, 5, 6]
[0, 1, 2, 3, 6, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5]
2
1
[5, 4, 3, 2, 1, 0]
list.sort(cmp=None, key=None, reverse=False)
Sort the items of list in place (the arguments for customization sorted() for their their explanation)
1. Rank 1 list. Sort by default in order from smallest to largest
L = [2,5,3,7,1]
L.sort()
print L
==>[1, 2, 3, 5, 7]
L = ['a','j','g','b']
L.sort()
print L
==>['a', 'b', 'g', 'j']
2.reverse is an bool value. The default is False, and if it is set to True, the elements in this list will be arranged in reverse order.
# reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
L = [2,5,3,7,1]
L.sort(reverse = True)
print L
==>[7, 5, 3, 2, 1]
L = ['a','j','g','b']
L.sort(reverse = True)
print L
==>['j', 'g', 'b', 'a']
3.key is a function that specifies the sort key, usually an lambda expression or a specified function
#key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).
#-*-coding:utf-8-*-
# create 1 containing tuple the list Among them tuple In the 3 Each element represents a name . height . age
students = [('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
print students
==>[('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
students.sort(key = lambda student:student[0])
print students
==>[('Dave', 180, 10), ('John', 170, 15), ('Tom', 160, 12)]# According to the name ( The first letter ) The sorting
students.sort(key = lambda student:student[1])
print students
==>[('Tom', 160, 12), ('John', 170, 15), ('Dave', 180, 10)]# Sort by height
students.sort(key = lambda student:student[2])
print students
==>[('Dave', 180, 10), ('Tom', 160, 12), ('John', 170, 15)]# Sorted by age
4.cmp is a function that takes two arguments. It determines how to sort.
#cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first #argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.
#-*-coding:utf-8-*-
students = [('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
print students
==>[('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
# The specified In the first 1 A capital letter (ascii code ) And the first 2 All lowercase letters (ascii code ) To compare
students.sort(cmp=lambda x,y: cmp(x.upper(), y.lower()),key = lambda student:student[0])
print students
==>[('Dave', 180, 10), ('Tom', 160, 12), ('John', 170, 15)]
# The specified Compare two letters in lowercase ascii Code value
students.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()),key = lambda student:student[0])
print students
==>[('Dave', 180, 10), ('John', 170, 15), ('Tom', 160, 12)]
#cmp(x,y) is python Inside to create a function for comparison 2 Two objects, if x < y return -1, if x == y return 0, if x > y return 1
cmp allows users to customize size relationships. Usually we think 1 < 2, think of a < b.
We can now customize the function by customizing the size relationship (for example, 2) < a < 1 < b) to sort list by specified rules.
This is often useful when we are dealing with particular problems.