A data structure is a collection of elements that are organized in a way that can be numbers or characters, or even other data structures. The most basic data structure in Python is the sequence. Each element in a sequence is assigned an ordinal-that is, the position of the element, also known as an index. The index of the first element is 0, the second is 1, and so on.
Python contains the built-in sequence in 6, and we'll focus on lists and tuples. The difference between the two is that the list can be modified and tuples cannot be modified.
General-purpose sequence operations, it is said that we are going to describe the operation for the sequence. All sequence types can perform certain operations. These operations include: Index, Shard, add, multiply, and check whether an element belongs to a member of a sequence.
1> Index
All elements in the sequence are numbered-incrementing from 0. These elements can be accessed by numbering. As follows:
>>> greeting= "Hello" >>> greeting[0] ' H '
A string is a sequence of characters. This is an index that gets the elements through an index, and all the sequences can be indexed in this way. When you index with negative numbers, Python counts from the right, and the index of the rightmost element is-1.
The literal value of a string can be indexed directly, without requiring a variable to refer to them.
>>> ' Hello ' [2] ' l ' >>> ' Hello ' [2] ' l ' >>> hello[2]/Such reference when the string must be quoted Tracebac K (most recent): File "<stdin>", line 1, in <module>nameerror:name ' Hello ' are not defined
If a function call returns a sequence, the returned result can be indexed directly.
>>> Nian=raw_input ("Year:") [3]year:2016>>> Nian ' 6 ' >>>
2> Shards
Using an index We can access a single element in a sequence, but using shards we can access a range of elements within a sequence. Shards are implemented by separating two indexes with a colon.
Shard operations are useful for extracting part of a sequence. and numbering here is especially important. The first index is the number of the 1th element to extract, that is, to extract the beginning of the range, the second index is the number of the second element, that is, the end of the fetch range, but the Shard contains the start side, not the end end.
>>> numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9] >>> Numbers[3:7]/extracts the range between the fourth element and the 7th element (4-8) but does not contain 8. [4, 5, 6, 7]>>> numbers[2:-1]/-1 The index represents the last element, but does not contain [3, 4, 5, 6, 7, 8]>>>
Let's say we need to access the last three elements:
>>> Numbers[6:9]/index 6 represents a 7th element, but index 9 represents the 10th element, which does not exist, but is not included. [7, 8, 9]>>> >>> numbers[-3:]/can also,-3 means the third-to-last element, until the end. [7, 8, 9]>>>
Access the top three elements:
>>> numbers[0:3][1, 2, 3]>>> numbers[: 3][1, 2, 3]>>>
Copy the entire sequence:
>>> numbers[:][1, 2, 3, 4, 5, 6, 7, 8, 9]>>>
If in a shard, the left element appears later in the sequence than the right element, then it becomes an empty sequence.
>>> numbers[6:2][]>>>
Greater stride size. At the time of the Shard, the flags for the start and end of the Shard need to be specified, and the other parameter-the step size-is usually implicitly set. In normal shards, the step is 1, and the Shard operation iterates through the entire sequence element in this step.
>>> numbers[0:5][1, 2, 3, 4, 5]>>> numbers[0:5:2][1, 3, 5]>>> numbers[5:0:-2]/step can also be negative, representing the right Extracts the element to the left. [6, 4, 2]>>>
3> sequence Addition
>>> [1, 2, 3] + [1, 5, 6][1, 2, 3, 1, 5, 6]>>> "Hello" + "World" ' HelloWorld ' >>> [1, 2, 3] + "Wo Rld "Traceback (most recent): File ' <stdin> ', line 1, <module>typeerror:can only concatenate Lis T (not ' str ') to list>>>
Lists and strings cannot be joined together, although they are all sequences. In simple terms, two sequences of the same type are required for the connection operation.
4> multiplication
Multiplying a number by a sequence generates a new sequence, and in the new sequence the original sequence is repeated n times.
>>> "Python" * 5 ' Pythonpythonpythonpythonpython ' >>> [*5[34], 34]>>>
An empty list can be simply represented by two brackets ([]), but if we want to create a sequence that takes up 10 element space, you can use none directly. None is a python built-in value, and its exact meaning is "nothing here".
>>> Sequence=[none] *10>>> sequence[none, none, none, none, none, none, none, none, none, none]>>& Gt
Checks whether a value is in the sequence, you can use the in operator, the operator returns a value, the result is true, otherwise false is returned.
>>> numbers[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> ' 3 ' in numbers/single quote for one character, so return false false>>> 3 I N numbers/Numeric means return true true>>>
Length, minimum value, maximum value:
>>> len (Numbers) 9>>> max (numbers) 9>>> min (numbers) 1>>>
2. List of methods:
A string cannot be modified like a list, so it is sometimes useful to create a list from a string.
>>> n=list ("Hello") >>> n[' h ', ' e ', ' l ', ' l ', ' O ']>>>
The list function is used for all types of sequences, not just strings.
Basic list operations:
1> changing the element assignment of a list:
>>> x=[1, 1, 1]>>> x[1, 1, 1]>>> x[1]=2>>> x[2]=3>>> x[1, 2, 3]>>>
2> Delete elements You can use the DEL statement:
>>> del x[2]>>> x[1, 2]>>>
3> Shard Assignment:
>>> name=list ("Perl") >>> name[' P ', ' e ', ' r ', ' L ']>>> name[2:]=list ("Ar") >>> name[ ' P ', ' e ', ' A ', ' R ']>>>
You can use a split-shard assignment to replace a shard with a sequence that is not equal to the original sequence.
>>> name[' P ', ' e ', ' A ', ' R ']>>> name[1:]=list ("Ython") >>> name[' P ', ' y ', ' t ', ' h ', ' o ', ' n ']&G T;>>
Shard assignments can also insert new elements without needing to replace any elements.
>>> numbers=[1, 5]>>> numbers[1:1]=[2, 3, 4]>>> numbers[1, 2, 3, 4, 5]>>>
This program simply replaces the Shard with empty, so the actual operation is to insert a sequence. If we replace the elements in the Shard with empty, we can achieve the purpose of the deletion.
>>> numbers[1, 2, 3, 4, 5]>>> numbers[1:3]=[]>>> numbers[1, 4, 5]>>>
3. List of methods:
A method is a function that is closely related to some object, which may be a list, a number, or a string or other type of object. In general, methods can be called this way.
Object. Method (Parameter)
The Append method is used to add new objects at the end of the list:
>>> lst=[1, 2, 3]>>> lst.append (4) >>> lst[1, 2, 3, 4]>>>
Note: When using append, it does not simply return a new list that has been modified--it modifies the original list directly.
Count method: Counts the number of occurrences of an element in a list
>>> ["To", "being", "or", "not", "to", "being"].count (' to ')/characters must be enclosed in quotation marks 2>>> ["to", "is", "or", "not", "to", ' Be '].count ("to") 2>>> x=[[1,2],1,1]>>> x.count (1) 2>>>
Extemd method: Appends multiple values from another sequence at the end of the list. In other words, you can extend the existing list with a new list:
>>> a=[1, 2, 3]>>> b=[4, 5, 6]>>> a.extend (b) >>> a[1, 2, 3, 4, 5, 6]>>>
Extend method: The extended sequence is modified, and the original connection operation returns a completely new sequence.
>>> b=[4, 5, 6]>>> a[1, 2, 3, 4, 5, 6]>>> a+b[1, 2, 3, 4, 5, 6, 4, 5, 6]>>> a[1, 2, 3, 4, 5, 6]>>> b[4, 5, 6]>>>
The index method. Used to find the index position of the first occurrence of a value from the list.
>>> n[' A ', ' B ', ' C ', ' d ', ' a ']>>> n.index ("a")/Note if it is a character, be sure to enclose it in quotation marks. 0
This article is from "Custom" blog, declined reprint!
Python---------lists and tuples