List of Python data structures, tuples, and dictionaries

Source: Internet
Author: User

A Daniel Niklaus Wirth once had a book, called "Algorithms+data Structures=programs", translated over the algorithm + data structure = program. This article is about the three types of data structures built into Python----lists, tuples, and dictionaries.

  List

  列表Is the data structure that handles a set of ordered items, that is, you can store a sequence of items in a list.

1.list function

1 >>>list ("Hello")2 ['H' ,'e'. ' L '. ' L '. ' o ']

  PS: You can use this method to convert a list of characters into a string:

1 '. Join (list)

  List is the name of the listing to convert.

2. Basic list Operations

    • Change list    
1 >>>x = [1,1,1]2 >>>x[1] = 23 >>>x 4 [1,2,1]
    • Delete Element
>>>names = ['Void','Alice','Jack  ']>>>del names[2]>>>names['  Void','Jack']
    • Shard Assignment
>>>name = List ('Perl')>>>name['P','e','R','L']>>>name[2:] = List ('ar')>>>name['P','e','a','R']

3. List method

    • a ppend

The Append method is used to append a new object to the end of the list:

1 = [>>>list]2 >>>list.append (4)3 >>>list4 [1,2,3.4]
    • Count

Count is used to count the number of occurrences of an element in a list:

1 >>>x = [[1,2],1,1]2 >>>x.count (1)3
    • Extend

The Extend method can append multiple values from another sequence at the end of the list at once. In other words, you can extend the existing list with a new list:

1 >>>a = [2] >>>b = [4,5]3 >>>  A.extend (b)4 >>>a5 [1,2,3,4,5]
    • Index

The index method is used to find the indexed position of the first occurrence of a value from a list:

1 >>>a = ['love','for','good  ']2 >>>a.index ('good') 3 3
    • Insert

The Insert method is used to insert an object into the list:

1 >>>a = [2 ] >>>a.insert (1,'four' )  )3 >>>a4 [1,'four', 2,3]
    • Pop

The Pop method removes an element from the list (the last one by default) and returns the value of the element:

1 >>>x = [4]2 >>>x.pop ()3 3 >> >x5 [up]
    • Remove

Remove is used to remove the first occurrence 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']
    • Reverse

The reverse method stores the elements in the list in reverse:

1 >>>x = [1,2.3]2 >>>x.reverse ()3 >> >x4 [3,2,1]
    • Sort

The sort method is used to sort the list at the original location:

1 >>>x = [4,6,2,1,7,9]2 >>>x.sort ()3 >>> x 4 [1,2,4,6,7,9]

  Meta-group

The main difference between tuples and lists is that the list can be modified and tuples cannot. In other words, if you add elements according to your requirements, the list may be more useful, and for some reason, it is more appropriate to use tuples when the sequence cannot be modified.

The way to create tuples is simply to separate values with commas, and you automatically create tuples.

1 >>>1,2,32 (All-in-all)

PS: So do you want to know how to implement a value tuple? You can guess the method is very peculiar-must have a comma, even if there is only one value:

>>>43, (>>>4343)

1.tuple function

  The function of the tuple function is basically the same as the list: Take a sequence as a parameter and convert it to a tuple

>>>tuple ([i]) (a)

  2. Basic meta-group operations

  The operation of tuples is not complex, and there are not many other operations besides creation and access.

  Dictionary

Lists such data structures are appropriate for organizing values into a structure and referencing them by number. The data structure of a dictionary, which is referred to as a value by name, is called mapping (mapping). Dictionaries are the only built-in mapping types in Python, and the values in the dictionary do not have a special order, but are stored under a specific key, which can be numbers, strings, or even tuples.

  1. Creating and Using dictionaries

1 >>>phone = {'Void':'123','  Allen':'321'}2 >>>phone['  Void']3'123'

  2. Basic dictionary operation

    • Len (d) Returns the number of items in D (key-value pairs)
    • D[K] Returns the value associated to the key K
    • D[K]=V associates the value V to the key K
    • Del D[k] Delete item with key K
    • K in D checks if D contains an entry with a key of K

List of Python data structures, tuples, and dictionaries

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.