Python data Structure-List, Tuple, Set, Dict

Source: Internet
Author: User
Tags set set

1. List

How to declare: List = [1,2.3, ' x ', ' Hello '], with method:

    • List.append (x) Add an item at the end of the list (append)
    • List.extend (L) to extend the current list (extended) with the given list
    • List.insert (I,X) Inserts an item at a given position
    • List.remove (x) Remove the first item in the list with the value X, Note that X is not an index
    • List.pop ([i]) deletes the item at the given position and returns
    • List.index (x) returns the index value of the first value of x in the list, with no match resulting in an error
    • List.count (x) returns the number of occurrences of x in the list
    • List.sort () sort
    • List.reserve () Reverse

To iterate through an instance:

numbers = [0,1,2,3,4,5,6,7,8,9]

For I in range (len (numbers)):

Print (Numbers[i])

2, tuple tuple

The Declaration method is very special, tuple = Item1,item2,item3,item4. For example:

tuple = 12,323,4,0,57, ' Hello '

For I in range (len (tuple)):

Print (Tuple[i])

3. Set Set

How to declare: Set = {ITEM1,ITEM2,ITEM3,ITEM4}, for example:

Basket = {' A ', ' B ', ' C ', ' d ', ' e '}

The collection is an unordered set of elements, and the result of the example declaration is:

{' E ', ' C ', ' d ', ' B ', ' A '}

The traversal method is:

For I basket:

Print (i)

4. Dictionary Dict

Declaration Example: tel = {' Jack ': 23432, ' scape ': 234}

The following methods can be used to assign values: tel[' chunyu '] = 19910805

The result is: {' Chunyu ': 19910805, ' Jack ': 23432, ' scape ': 234}

You can use the items () method to get the keys and corresponding values, for example:

For k,v in Tel.items ():

Print (K,V)

The traversal method is:

Tel = {' Chunyu ': 19910805, ' Jack ': 23432, ' scape ': 234}

For key in Tel:

Print (Key, ': ', Tel[key])

Python data Structure-List, Tuple, Set, Dict

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.