Introduction:
What is a data structure ?
data results are collections of data elements that are organized in some way, such as numbering elements . The most commonly used data structure in Python is the sequence .
Python contains 6 built-in sequences: lists and tuples (most commonly used: Lists can be modified, tuples are not), strings, Unicode strings, buffer objects, and Xrange objects.
One, the general operation of the sequence:
1, the index (from left to right to start with 0 increments, from right to do-1 start continuously decreasing)
>>> greeting = ' Hello '
>>> Greeting[0]
' H '
>>> Greeting[-1]
' O '
2. Shard (the first of the elements before the beginning element is a colon and the ending element is a colon)
>>> Greeting[0:3] (0,1,2)
' Hel '
>>> Greeting[-3:-1] ( -3,-2)
' ll '
>>> greeting[-3:] (-3, to the end)
' Llo '
>>> greeting[:]
' Hello '
>>> Greeting[1:3:2] #以2为步长, the front is the default size of 1
E
3, sequence addition (only the same type can be added)
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> ' Hello ' + ' world '
' HelloWorld '
>>> [1,2,3]+ ' World '
Traceback (most recent):
File "<pyshell#10>", line 1, in <module>
[1,2,3]+ ' World '
Typeerror:can only concatenate list (not "str") to List
4. Multiplication
>>> ' python '
' Pythonpythonpythonpythonpython '
>>> [56]*4
[56, 56, 56, 56]
>>> sequence = [None]*10 #空列表 [None]
>>> sequence
[None, None, none, none, none, none, none, none, none, none]
5. Membership (use the In operator to check if a value is in sequence)
>>> ll = ' 8UUSDF '
>>> ' U ' in ll
True
>>> ' we ' in LL
False
>>> ' Uus ' in LL
True
>>> ' US ' in LL
True
>>> ' UD ' in LL
False
6, length, minimum and maximum (len (sequence name) with a lot of)
>>> numbers = [89, 56, 100]
>>> Len (Numbers)
3
>>> Max (Numbers)
100
>>> min (Numbers)
56
Ii. list: Python's "menial" (here's a useful and exclusive way to tell the list)
(a) Basic operation
1. List method (The list method converts a string into a list form; A join can connect a list to a symbol, such as a string by "empty character list").
>>> list1 = list (' Hello ')
>>> List1
[' H ', ' e ', ' l ', ' l ', ' O ']
>>> ". Join (List1)
' Hello '
>>>
2. Assigning values to elements
>>> x = [+ +]
>>> X[1] = 6
>>> x
[1, 6, 3]
3. Deleting elements
>>> del X[1]
>>> x
[1, 3]
3. Partition Assignment Value
>>> name = List (' I Love you ')
>>> Name
[' I ', ', ' l ', ' o ', ' V ', ' e ', ', ' y ', ' o ', ' u ']
>>> Name[2:6] = list (' hate ')
>>> Name
[' I ', ' ', ' h ', ' a ', ' t ', ' e ', ', ' y ', ' o ', ' u ']
>>> ". Join (name)
' I hate you '
The following elements are inserted without replacing any of the elements:
>>> numbers = [1,5]
>>> numbers[1:1] = [2,3,4]
>>> numbers
[1, 2, 3, 4, 5]
The following is the deletion of an element without replacing any of the elements:
>>> numbers
[1, 2, 3, 4, 5]
>>> Numbers[1:5] = []
>>> numbers
[1]
(ii) List Method-important (note define the name of the variable or object, do not have the same name as the built-in function or the methods of some classes, or it will cause the call to fail)
1,append--Append a new object at the end of the list (will change the original list)
>>> LST = [+]
>>> Lst.append (4)
>>> LST
[1, 2, 3, 4]
2,count--counts the number of occurrences of an element in a list
>>> x = [[1,2],2,3,[1,2],1,1]
>>> X.count (1)
2
>>> X.count ([up])
2
3.Extend-Append multiple values at the end (will change the original list)
>>> a = [+ +]
>>> B = [4,5,6]
>>> A.extend (b)
>>> A
[1, 2, 3, 4, 5, 6]
If you use the + operation, the value of the original list is not changed:
>>> a = [+ +]
>>> B = [4,5,6]
>>> a+b #生成新的列表
[1, 2, 3, 4, 5, 6]
>>> A
[1, 2, 3]
4,index--Find out the index position of the first occurrence of a value from the list
>>> KK = [' What ', ' is ', ' your ', ' name ', '? ']
>>> Kk.index (' is ')
1
>>> KK2 = [' What ', ' was ', ' ll ', ' is ', ' Doudou ']
>>> Kk2.index (' is ') #注: There are two ' is ' in the list, the index will be looked from left to right, found to return directly, that is, find the first stop, if not, will return the error
1
>>> kk2.index (' wer ')
Traceback (most recent):
File "<pyshell#16>", line 1, in <module>
Kk2.index (' wer ')
ValueError: ' wer ' isn't in list
5,Insert--Inserts the object into the list (the parameter is < insert position, the value of the object, the insertion position is calculated from 0, where you want to insert, just write the index value of the position)
>>> numbers = [1,2,3,5,6,7]
>>> Numbers.insert (3, ' four ')
>>> numbers
[1, 2, 3, ' Four ', 5, 6, 7]
6,pop-Removes an element from the list, the default is the last
Note: The Pop method is the only list method that can modify both the list and return the element value (except none)
>>> x = [+ +]
>>> X.pop ()
3
>>> x
[1, 2]
>>> x.pop (0) #默认是最后一个, here you can specify the parameter, which is the index value
1
>>> x
[2]
7.Remove-Removes the first occurrence of a value in the list (based on the value, not the index value) "In contrast to a pop, there is no way to change the original position of the return value"
>>> x = [' To ', ' is ', ' or ', ' not ', ' to ', ' is ']
>>> x.remove (' be ')
>>> x
[' to ', ' or ', ' no ', ' to ', ' being ']
>>> x.remove (' be ')
>>> x
[' to ', ' or ', ' no ', ' to ']
>>> x.remove (' 234 ')
Traceback (most recent):
File "<pyshell#11>", line 1, in <module>
X.remove (' 234 ')
ValueError:list.remove (x): X not in List
8,reverse--reverse the elements in the list (change the list without returning a value)
>>> x = [+ +]
>>> X.reverse ()
>>> x
[3, 2, 1]
9.Sort-Sort method: Sort the list in the original location
>>> x = [3,5,2,7,1,10,6]
>>> X.sort ()
>>> x
[1, 2, 3, 5, 6, 7, 10]
If you need to preserve the value of x, you can assign a copy of x to Y and then sort y
>>> x = [3,5,2,7,1,10,6]
>>> y = x[:] #注, must be assigned in this way, not directly y = X
>>> Y.sort ()
>>> y
[1, 2, 3, 5, 6, 7, 10]
>>> x
[3, 5, 2, 7, 1, 10, 6]
The following is the case of direct y = x:
>>> x = [3,5,2,7,1,10,6]
>>> y = x
>>> Y.sort ()
>>> y
[1, 2, 3, 5, 6, 7, 10]
>>> x
[1, 2, 3, 5, 6, 7, ten] #这里的y和x指向的是同一个列表了
You can also pass the sorted () function with the following code:
>>> x = [3,5,2,7,1,10,6]
>>> y = sorted (x)
>>> y
[1, 2, 3, 5, 6, 7, 10]
>>> x
[3, 5, 2, 7, 1, 10, 6]
10,CMP--Compare size, CMP (x, y), × greater than Y will return 1;x less than y return 0
>>> CMP (42,32)
1
>>> CMP (99,100)
-1
>>> numbers = [5,2,9,7]
>>> Numbers.sort (CMP)
>>> numbers
[2, 5, 7, 9]
Three, meta-group
1, the definition of the tuple:
>>>
(1, 2, 3)
>>> 42,
(42,)
>>> ()
()
>>> (40+2)
126
>>> (40+2,)
(42, 42, 42)
2, the tuple () function--Similar to the list method for lists, you can convert a list or string into a tuple by using this function
>>> tuple ([+])
(1, 2, 3)
>>> tuple (' abc ')
(' A ', ' B ', ' C ')
>>> tuple ((+))
(1, 2, 3)
3, the basic operation of the tuple--unlike the list, there is no list of a bunch of methods, the basic operation of tuples (create tuples and access tuple elements) can see other types of sequences
>>> x =
>>> X[1]
2
>>> X[0:2]
(1, 2)
"Basic Python Tutorial 2nd Edition"-Second lecture: Lists and tuples