The name of this chapter is called lists and tuples, but the most part of this chapter is the list, and Ganso refers to a very small number of sections. Because tuples and lists are the same in many ways.
The difference between a list and a tuple: The list can be modified and the meta-ancestor cannot be modified.
Python contains 6 built-in sequences: list, tuple, string, Unicode string, buffer object, Xrange object.
The built-in function returns a tuple because the tuple cannot be modified.
The form of the list is [], and the elements are separated by commas.
Sequences, such as lists and tuples, and mappings, such as dictionaries, are two main types of containers. Each element in the sequence has its own number, and each element in the map has a name (also known as a build).
Collection (set) is neither a sequence nor a mapping type
Common sequence Operations: index (indexing), shard (slicing), plus (adding), multiply (multiply), Iteration (iteration)
Iteration (Iteration): Repeats some operations on each element of the sequence in turn.
Index (indexing): All elements in a sequence are numbered and numbered starting with 0. When using a complex index, Python starts counting from the right, that is, the last element, and the last element has a position of-1.
Cases:
>>> ' Hello ' [1]
>>> ' E '
>>> input (' year: ') [3]
year:2016
>>> ' 6 '
Python3 uses input instead of the raw_input in Python2
Shard (slicing): shards are implemented by separating two indexes with colons. The element of the first index is contained within the Shard, and the second does not contain one.
num[First Index: Second index: Step size]
For example:
>>>num = [1,2,3,4,5,6]
>>>num[1:4:2]
[2,4]
The first index, the second index, and the step size can all be negative numbers. The step size cannot be 0, which is 0 when not executed.
When using a negative number as a step, you must let the start point (Start index) be greater than the end point.
Add (Adding): List and string cannot be joined together. Only two sequences of the same type are allowed to connect.
For example: [4,5,6] + [the] result is [1,2,3,4,5,6]
' Hello ' + ' world ' results for ' Hello World '
[[+] + ' Hello ' error
Multiply (multiply): by multiplying the number x by a sequence, a new sequence is generated, and the original sequence is repeated x times in the new sequence.
For example: ' He ' * 5 ' hehehehehe '
[1] * 5 [1,1,1,1,1]
[] denotes an empty list with nothing in it.
None is a python built-in value, and its exact meaning is "there's nothing here."
[None] * 5 [None,none,none,none,none]
The//representation in the Python3 is divisible,/represents the exception, regardless of whether the value is integer or floating-point. The default/representation divide in Python2.
Membership: In
In order to check whether a value is in a sequence, you can use the in operator.
For example: >>>st = [' as ', ' df ', ' gh '] (List members can also be lists, so that when viewing a list is in the list)
>>> ' as ' in St
True
>>> ' BB ' in St
False
>>> ' st ' in ' string '
True
Length, Min, Max: Len (), Min (), Max ()
Len (), Min (), and Max () belong to the built-in function.
Len () returns the number of elements contained in the sequence.
Min () and Max () return the smallest and largest elements in the sequence.
For example:>>> num = [3,4,2,5,1]
>>>len (num)
5
>>>max (num)
5
>>>min (num)
1
>>>max (2,3,5,1)
5
The parameters for Max () and Min () can be not only a sequence, but also a number of parameters directly.
List:
List function: You can convert all the sequences to a list.
Example: >>>list (' Hello ')
[' H ', ' e ', ' l ', ' l ', ' O ']
Note: List is used for all types of sequences, not just strings.
Basic list operations:
Lists can use standard actions that apply to sequences, such as indexes, shards, links, multiplication.
Element assignment: You can assign a value directly using the subscript method, and you cannot assign a value to an element that does not exist. x = [X[1] = 5 result x=[1,5,3], the subscript of an assignment index cannot exceed 2, and more than 2 is an element that does not exist.
Delete element: It is implemented with the DEL statement. Del x[1] Results x=[1,3]. Del can be used for many variables, not just a list, but also a dictionary.
Shard Assignment: It allows the program to assign values to multiple elements at once.
When using shards, you can replace shards with sequences that are not equal to the original sequence.
>>> name = List (' Perl ')
>>> name[2:] = list (' python ')
>>> Name
[' P ', ' e ', ' P ', ' y ', ' t ', ' h ', ' o ', ' n ']
Shard Assignment allows you to insert new elements without replacing any elements
>>> num = [1,5]
>>> num[1:1] = [2,3,4]
>>> num
[1,2,3,4,5]
Similarly, shards can also delete elements
>>> Num[1:4] = []
>>> num
[1,5]
Of course, the above operations can also be added in step size.
List methods: Append, count, Extend, index, insert, pop, remove, reverse, sort, advanced sort.
Append: Used to append a new object to the end of the list. LST = [Lst.append] (4) Results LST = [1,2,3,4], the list itself is modified.
Count: Counts the number of occurrences of an element in the list. [[Up], [1,1],1,1,[2,3]].count ([]] The result is 1, [[], [1,1],1,1,[2,3]].count (1) The result is 2
Extend: You can append multiple values from another sequence at the end of a list. a=[1,2,3] b=[4,5,6] A.extend (b) result A = [1,2,3,4,5,6]. The value of the list itself is modified. If you use the + sign to connect two lists, you can also get the same value, but the list itself is not modified, but the return value is the same. The result of a + B is [1,2,3,4,5,6], but a is still [all-in-one]
Index: From the list, find the indexed position of the first occurrence of a value. An exception occurs when there is no such element. st = [' qw ', ' er ', ' er ', ' er ', ' SD '] st.index (' er ') result is 1
Insert: For inserting an object into the list. St.insert (2, ' a ') results in st = [' qw ', ' er ', ' a ', ' er ', ' er ', ' SD '], and the list itself is modified.
Pop: Removes an element from the list, which is the last element by default, and returns the value of the element. x=[1,2,3,4] X.pop () Results 4 x.pop (0) Results 1. Pop is the only method that can modify a list and return elements that are worth the list.
Remove: Removes the first occurrence of a value in the list. St.remove (' er ') results st = [' QW ', ' A ', ' er ', ' er ', ' SD ']. This method is a method of changing the original position without a return value.
Reverse: The elements in the list are stored in reverse. A.reverse () a=[3,2,1]. The method also changes the list but does not return a value (just like remove and sort)
Sort: This method is used to sort the list at the original location. The original list was changed, not simply returning a sorted copy of the list. The method has no return value.
>>> x = [3,1,2,4]
>>> X.sort (); X
[1,2,3,4]
>>> y = x.sort () error, the method does not return a value. The value of Y is None
If you just want to get a sorted copy of the list, the original list remains, and you can use the following method only:
>>> y = x[:] It is not easy to use Y=X to assign a value, so that Y also points to the value of x, and when x changes, y also changes. X[:] is a shard that contains all the elements of X, which is a very efficient way to assign an entire list
>>> Y.sort () uses a copy to sort the original list without modifying it.
Also use the sorted function:
>>> y = sorted (x)
At this point, the value of Y is [1,2,3,4] and the value of x is still [3,1,2,4]
The sorted function can be used not only for lists, but also for any sequence, always returning a list:
>>> sorted (' python ')
[' H ', ' n ', ' o ', ' P ', ' t ', ' Y ']
Advanced Sort: The Sort method has two optional parameters, key and reverse.
If you want to reverse the sort result, you can adjust the parameter x.sort (reverse = True).
If you want to sort by length, you can use Len, which is x.sort (key = len)
Tuples: Immutable sequences
If you use commas to separate values, you automatically create a tuple.
>>>
(a)
Tuples are mostly enclosed in parentheses, and empty tuples can be represented with two parentheses () without content.
Include a worthy tuple, not a simple value, but a comma after the value.
>>> 3 * (42)
126
>>> 3 * (42,)
(42, 42, 42)
Tuple function: You can convert a sequence as an argument to a Narimoto group. If the parameter is a tuple, the parameter is returned as is.
>>> tuple ([+])
(a)
>>> tuple (' abc ')
(' A ', ' B ', ' C ')
>>> tuple ((+))
(a)
Python Basic Tutorial Notes chapter II