Chapter 1 important data structures in python (I) and python Data Structures

Source: Internet
Author: User

Chapter 1 important data structures in python (I) and python Data Structures

Recently, due to work needs, I used the O & M automation platform of the python development company. So I found this book and started my study on python in conjunction with the official manual.

I. List

[Meaning ]:The list is represented in parentheses and separated by commas (,) (different data types can be used), as stated in the following statement:

1 >>> language = ['Chinese ', 'English', 'Japanese ese '] 2 >>> contries = ['China', 'amercia', 'England ', 'Japan '] 3 4 >>> edward = ['edward gumby', 42] # different data types 5 >>> john = ['John Smith ', 50] 6> database = [edward, john] # The list can be nested.

[Operation]: Access, insert, delete, and calculate the maximum value, minimum value, and length.

1. Access: You can use indexes or shards to traverse the list, and the index can access the value at a certain position. The sharding can flexibly access a certain range of values, as shown below:

1 >>> language = ['China', 'inc ', 'nginx'] 2 # index access 3> language [0] 4 'China' 5 6 # partition access 7 # The first index element is included in the partition, the second index is not included in the Part 8 # You can set the part Step 9> language [:] 10 ['China', 'inc ', 'nginx'] 11 >>> language [0: 3] 12 ['China', 'English ', 'nginx'] 13 >>> language [0: 2] 14 ['China ', 'inc'] 15 >>> language [] 16 ['inc'] 17 # Set the step size to 218 >>>> num = [1, 2, 3, 4, 5, 6, 7, 8, 9] 19 >>> num [0: 9:2] 20 [1, 3, 5, 7, 9]

21 >>> num = [1, 2, 3]
22 >>> max (num)
23 3
24 >>> min (num)
25 1
26 >>> len (num)
27 3

2. You can modify, insert, or delete a list.

1 # modify an element in the list: Index Assignment 2> num = [1, 2, 3] 3> num [0] = 5 # assign a value to an existing location index, otherwise, the error 4 >>> num 5 [5, 2, 3] 6 7 # modify the value of a range in the list: partition assignment 8 >>> num [2:] = [6, 7, 9] # The number of partition assignment elements can be 9> num10 [5, 2, 6, 7, 9] 11 12 # delete an element 13> del num [4] 14> num15 [5, 2, 6, 7]

[Method]For the list, python has many built-in methods for operation. The following are commonly used:

1 # append method: Used to add elements at the end of the list. This method directly modifies the original list and returns the new list after modification. 2 >>> str = ['A ', 'B', 'C', 'D'] 3 >>> str. append ('E') 4 >>> str 5 ['A', 'B', 'C', 'D', 'E'] 6 7 # count method: count the number of times an element appears in the list. 8 >>> str = ['A', 'B', 'C', 'D'] 9 >>> str. count ('A') 10 111 12 # extend method: list. extend (L), L refers to the list object 13 # using another list to expand a list is equivalent to two list connections, 14 # but different from the connection operation, because the extend method directly modifies the original list and returns the result, 15 # The Connection operation does not affect the original list value 16 >>> str1 = ['hello, '] 17 >>> str2 = ['World'] 18 >>> str1.extend (str2) 19 >>> str120 ['hello ,', 'World'] 21 22 >>> str3 = ['a'] 23 >>>> str4 = ['5'] 24 >>>> str3 = str3 + str4 # efficiency without extend high 25 >>> str326 ['A ', '5'] 27 28 # index method: returns the first index location of a match. 29 >>> knight = ['we', 'you', 'we ', 'Me', 'hes'] 30 >>> knight. index ('we') 31 032 33 # insert method: list. insert (I, x) insert x to the position 34> knight. insert (1, 'she') 35 >>> knight36 ['we', 'she', 'you', 'we', 'me ', 'hes'] 37 38 # remove Method: list. remove (x) 39 # Delete the first occurrence element of X in the list. 40 >>> knight = ['we', 'she', 'you', 'we ', 'Me', 'hes'] 41 >>> knight. remove ('we') 42 >>> knight43 ['she', 'you', 'we', 'me', 'hes'] 44 45 # reverse method: store elements in Reverse Order 46 >>> str = ['A', 'B'] 47 >>> str. reverse () 48 >>> str49 ['B', 'a'] 50 51 # sort method: list. sort (key = None, reverse = False) 52 # directly change the order of the original list, instead of changing the copy. For this requirement, such as 53 # Simply sort the copy, you cannot directly use sort54> num = [1, 3, 2, 4] 55> num without changing the original list. sort () 56 >>> num57 [1, 2, 3, 4] 58 59 # pop method: list. pop ([I]) 60 # Delete the element value at the specified index location and return the value 61 >>> num = [1, 2, 3, 4] 62 >>> num. pop (3) 63 4

[Examples ]:Simulate stack operations and queue operations

STACK: Back-to-first-out

 1 >>> stack = [3, 4, 5] 2 >>> stack.append(6) 3 >>> stack.append(7) 4 >>> stack 5 [3, 4, 5, 6, 7] 6 >>> stack.pop() 7 7 8 >>> stack 9 [3, 4, 5, 6]10 >>> stack.pop()11 612 >>> stack.pop()13 514 >>> stack15 [3, 4]

Queue: FIFO

 1 >>> from collections import deque 2 >>> queue = deque(["Eric", "John", "Michael"]) 3 >>> queue.append("Terry")           # Terry arrives 4 >>> queue.append("Graham")          # Graham arrives 5 >>> queue.popleft()                 # The first to arrive now leaves 6 'Eric' 7 >>> queue.popleft()                 # The second to arrive now leaves 8 'John' 9 >>> queue                           # Remaining queue in order of arrival10 deque(['Michael', 'Terry', 'Graham'])

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.