Objective:
Lists are the most basic data structures in Python, and the list is the most commonly used Python data type, and the list's data items do not need to have the same type. Each element in the list is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
Python has 6 built-in types of sequences, but the most common are lists and tuples. Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.
1. list: Create a list, get the value of the list by index
Note: The index is starting from 0
>>> arr = [' C ', ' a ', ' a ', ' B ', true,false,[1,2,[3,5]], ' python ']>>> print arr[0]c>>> print arr[-1]python>>> Print arr[6]true>>> Print arr[8][2][0]3
2. use in to determine if a value string exists in a list
>>> L = list (' ABCDEFG ') >>> l[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']>>> ' a ' in ltrue>>> ' Z ' In Lfalse
3, to achieve the function of getting list length
Num_list=[1,2,3,[4,5,6]]i = 0for J in Num_list:i + 1print I
4. Python's built-in function on list
Len (): Returns the length of the list
Max (): Returns the maximum value
MIN (): Returns the minimum value
Del (): delete element
>>> a = [1,2,3,4,5]>>> len (a) 5>>> Max (a) 5>>> min (a) 1>>> del (a[1]) > >> a[1, 3, 4, 5]
Note: Do not conflict with built-in function names when writing functions or defining variables
5. Modify the value specified in the list by the index implementation
>>> a = [2,3,4,1,5,6]>>> a[0] = 1>>> a[1, 3, 4, 1, 5, 6]>>> a[-1] = 10>>> a[1 , 3, 4, 1, 5, 10]
6. Bubble sort
L = [3,4,33,26,1]for i in L:for J in Range (Len (l)-1): If l[j]>l[j+1]: L[j],l[j+1]=l[j+1],l[j]prin T L
#优化后l = [3,4,33,26,1]for i in Range (Len (l)-1): For j in Range (Len (l) -1-i): If l[j]>l[j+1]: l[j],l[ J+1]=l[j+1],l[j]print L
7, List slicing (powerful, but poor readability)
Slicing principle: Start with the direction of the slice and find the end point
[1:4] 1 is the starting point, and 4 is the end point.
[:] Take all
[:: 2] take one from one
[::-1] from right to left
[:-1] The last one does not take
>>> arr = list (' ABCDEFG ') >>> print arr[2:][' C ', ' d ', ' e ', ' f ', ' G ']>>> print arr[:5][' A ', ' B ', ' C ', ' d ', ' E ']>>> print arr[:5:2][' A ', ' C ', ' E ']>>> print arr[4:1:-1][' e ', ' d ', ' C ']
8, List of several methods
Append (): Append element
COUNT (): Number of Statistics element occurrences
Extend (): Extended source list
Index (): Find Worthy Index
#append () method >>> A = [1,2,3]>>> B = 5>>> A.append (b) >>> a[1, 2, 3, 5] #count () method >> > A = [1,2,3,2,2,1]>>> a.count (1) 2#extend () method >>> A = [1,3]>>> B = [2,4]>>> A.exten D (b) >>> a[1, 3, 2, 4] #index () method >>> a = [' A ', ' B ', ' C ']>>> a.index (' a ') 0
9. Insert () method
The Insert () method inserts a value at the specified index position
>>> a = [' Hello ', '! '] >>> a[' hello ', '! '] >>> A.insert (1, ' World ') >>> a[' hello ', ' world ', '! ']
10. Pop () method
Pop () method is the popup element, the default popup the right one element
>>> a = list (' ABCDEFG ') >>> a[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']>>> a.pop () ' G ' >>> a.pop (0) ' a '
11. Data structure
Queue: First in, first out
Append () and pop (0) can simulate the queue's first in, first out
Stack: first in and out
Append () and pop (0) can simulate the first in and out of the stack
Array:
Depending on the index, the probability of hitting the CPU cache is very fast, from the last append.
Insert is slow, delete is slow
Linked list:
Shaped like: 1->2->3->4->5
The list is very slow to find, quick to insert, and quick to delete. The tail addition is quick.
Hash table:
Relationships can be established by specific values, such as dictionaries in Python
No order, find, delete, modify all quickly
Hash types are: FNV hash, MD5 hash, consistent hash, etc.
Practice:
1. a sequence [1,2,3,4,2,12,3,14,3,21,2,2,3,4111,22,3333,4]
The index value for the second 4
A = [1,2,3,4,2,12,3,14,3,21,2,2,3,4111,22,3333,4]b = A.index (4) a_index4 = A.index (4,b+1) Print a_index4
2, to-do items, let users continue to input, using the queue
If the user input is add, let the user enter the characters, add to the to-do list, let the user continue to enter, if the user input is do, from the agent to print out a matter, if there is no matter to do, terminate the program
Sth = []while true: action = raw_input ("Please input your type, ' Add ' or ' do ': ") if action== ' Add ': thing = raw_input ("Please input thing you want to do: ") sth.append (thing) elif action== ' Do ': if sth: todo = sth.pop (0) print todo else: print ' Nothing to do! ' else: break
3, the array to weight
[1,2,3,4,2,12,3,14,3,2,12,3,14,3,21,2,2,3,4111,22,3333,4]
List_a = [1,2,3,4,2,12,3,14,3,2,12,3,14,3,21,2,2,3,4111,22,3333,4]list_b = []for i in List_a:if I not in List_b: List_b.append (i) Print list_b
4, the array to weight
ARR1 =[1,2,3,4,2,12,3,14,3,2,12,3,14,3,21,2,2,3,4111,22,3333,4]
ARR2 = [2,1,3,2,43,234,454,452,234,14,21,14]
Two array common values (need to go to the weight)
List_a = [1,2,3,4,2,12,3,14,3,2,12,3,14,3,21,2,2,3,4111,22,3333,4]list_b = [2,1,3,2,43,234,454,452,234,14,21,14] New_list = []for i in List_a:if i in list_b and I not in new_list:new_list.append (i) Print new_list
5, two-point idea: In a list of rows, find the specified number, and show the number of lookups
ARR = Range (10000) find = input (' Please enter a number: ') start = 0end = Len (arr) -1count = 0while True:count + = 1 mi D = (start+end)/2 if FIND>ARR[MID]: start = Mid elif Find<arr[mid]: End = Mid ELSE:PR int Mid Breakprint Count
My Python study-the next day (list)