List, tuples, and dictionaries of PYTHON Data Structures

Source: Internet
Author: User

List, tuples, and dictionaries of PYTHON Data Structures
Niklaus Wirth, a Daniel, once published a book named "Algorithms + Data Structures = Programs", which is translated as an algorithm + Data Structure = program. This article describes the three built-in data structures in Python-list, tuples, and dictionaries. A list is a data structure that processes a group of ordered projects. You can store a series project in a list. 1. the list function cannot be modified like a list. Sometimes it is useful to create a list based on a string. You can perform the following operations on a list: 1 >>> list ("Hello ") 2 ['h', 'E '. 'l '. 'l '. 'o'] PS: You can use the following method to convert a list composed of characters to a string: 1 ''. join (list) list is the name of the list to be converted. 2. basic List Operation Change List 1 >>> x = [, 1] 2 >>> x [1] = 23 >>> x4, 1] delete an element >>> names = ['void', 'Alice ', 'jack'] >>> del names [2] >>> names ['void ', 'jack'] partition assignment >>> name = list ('perl ') >>> name ['P', 'E', 'R ', 'L'] >>>> name [2:] = list ('ar ') >>> name ['P', 'E', 'A ', 'R'] 3. the list method append is used to append a new object to the end of the list: 1 >>> list = [1, 2, 3] 2 >>> list. append (4) 3 >>> list4 [3.4,] count is used to count the number of times an element appears in the list: 1 >>> x = [[1, 2], 1, 1] 2> x. count (1) 3 2 ext The end extend method can append multiple values in another sequence at a time at the end of the list. In other words, you can use the new list to expand the original list: 1 >>> a = [, 3] 2 >>> B = [] 3 >>>. extend (B) 4> a5 [, 5] index method is used to locate the index location of the first matching item of a value from the list: 1 >>> a = ['love', 'for', 'good'] 2 >>>. index ('good') 3 3 insert method is used to insert an object to the list: 1 >>> a = [1, 2, 3] 2 >>>. insert (1, 'four ') 3 >>> a4 [1, 'four', 2, 3] The pop method removes an element from the list (the last one by default ), and return the value of this element: 1 >>> x = [1, 2, 3] 2 >>> x. pop () 3 34 >>> x5 [] remove is used to remove the first matching item of a value in the list: 1 >>> x = ['to ', 'be ', 'Or', 'not ', 'to', 'be'] 2> x. remove ('be') 3 >>> x4 ['to', 'or', 'not, 'to ', 'be'] The reverse method reversely stores the elements in the list: 1 >>> x = [1, 2.3] 2 >>> x. the reverse () 3 >>> x4 [, 1] sort method is used to sort the list in the original position: 1 >>> x =, 7, 9] 2> x. the main difference between sort () 3 >>> x4 [,] tuples and lists is that the list can be modified, but not the tuples. That is to say, if you add elements as needed, the list may be easier to use. For some reason, it is more appropriate to use tuples when the sequence cannot be modified. The method for creating tuples is simple. You only need to use commas to separate some values, and then you will automatically create tuples. 1 >>> 1, 2, 32 (1, 2, 3) PS: Do you want to know how to implement a value tuples? You can guess, the method is very strange-there must be a comma, even if there is only one value: >>> 43, (43) >>> 4343 1. the tuple function has the same functions as the list function: Use a sequence as a parameter and convert it to a tuples >>> tuple ([, 3, 3) 2. operations on basic tuples are not complex. There are not many operations except creation and access. Dictionary lists are suitable for organizing values into a structure and referencing them by numbers. The data structure of the dictionary refers to the data structure of the value by name, which is called mapping ). The dictionary is the only built-in ing type in Python. The values in the dictionary do not have any special sequence, but are stored under a specific key. The key can be a number, string, or even a tuples. 1. create and use dictionaries 1 >>> phone = {'void': '000000', 'allen ': '000000'} 2 >>> phone ['void'] 3 '000000' 2. basic dictionary operation len (d) returns the item (key-Value Pair) in d) d [k] returns the value d [k] = v associated with the key k. d [k] = v associates the value v with the key k. del d [k] deletes the key k. in d check whether d contains the key k

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.