The sequence is the most basic and most important data structure in Python, with 6 sequence types built into Python, such as list, tuple, string, Unicode, buffer, xrange
Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on. And the sequence can be indexed, check members, slices, add, multiply and so on.
List
List is a mutable data type
1. Create a simple list:
A = [' A ', ' B ', ' C ']
b = [1,2,3,4,,5]
2, read the value of the list:
A[2]
The result is C
3. Delete:
Del (a[2])
Del (a)
4, the list of nested:
A = [' A ', ' B ', [' C ', ' d '], ' e ']
5, the list of slices (emphasis)
For data with a sequence structure, the slice operation is: Consequence[start_index:end_index:step].
A = [1,2,3,4,5,6,7,8]
A[2:]
The result is: [3,4,5,6,7,8]
A[2:5]
The result is: [3,4,5]
A[2::2]
The result is: [3,5,7]
A[2:2:2]
The result is: [3,5]
A[::-1]
The result is: [8,7,6,5,4,3,2,1]
6, List support "+", "*"
List of common methods:
List.append () Adds a new element to the end of the list
List.inster (index,obj) adds obj to the index position
List.extend (seq) adds more than one sequence (not necessarily a list) to another series in the list
A = [1,2,3]b = [' A ', ' B ', ' C ']a.extend (b[:2:]) The result is: [A-D, ' A ', ' B ']
List.count (obj) Statistics The number of occurrences of obj in the list
List.pop ([index]) removes the last (selectable index) element in the list and returns the value
List.remove (obj) deletes the first matching obj in the list
List.reverse () Reverse Output list
List.sort () sort the list
Meta-group
Tuples are similar to lists, except that tuples cannot be modified, the list uses "[]" when defined, tuples use "()", dictionaries use "{}", and the collection uses "{}"
1. Define tuples:
A = (' A ', ' B ', ' C ',)
() represents an empty tuple
(' A ',) represents only one element of a tuple, where "," Cannot save
' A ', ' B ', ' C ' also indicates that the tuple is just not plus (), but in order to prevent confusion generally add ()
2, tuple support "+"
A = (up) b = (3,4) c = a+b result is: (1,2,3,4)
3, tuples support slicing
Python list, tuple