Python_list,tuple,dict,set

Source: Internet
Author: User
Tags set set

One: List listing

List is an ordered set of elements that can be added and removed at any time

By  [ ]  enclosing all the elements of the list, it is a list object. In general, we assign a list to a variable so that it can be referenced by a variable to the list

classmates = [' Michael ', ' Bob ', ' Tracy ']

Query element:

Positive order: The index starts at 0, that is, the index of the first element is 0, the second element is indexed by 1, and so on

Reverse: Use the index of 1 to represent the last element, the penultimate 2 means, the third use-3 means, the penultimate fourth with-4 means

To add an element:

(1.) append() method to append the new element to the end of the list

 (2.) insert()method, which takes two parameters, the first parameter is the index number, and the second parameter is the new element to be added. The original index and all subsequent elements are automatically moved backwards one

To delete an element:

The pop () method always deletes the last element of the list, and it returns this element, so we will print out ' xxx ' after we execute L.pop ()

To delete the element at the specified location, 可以使用pop(索引),例如:pop(2)

Replace element:

To assign a value to an index in the list, you can replace the original element with the new element, and the list contains the same number of elements.

Two: Tuple tuples

By () enclosing all the elements of a tuple, it is a tuple object, and the tuple is an ordered set.

After a tuple is created, it cannot be modified, so the tuple does not have a append () method, nor does the insert () and Pop () methods

The way to get a tuple element is exactly the same as the list, and we can access the element normally using indexed methods such as t[0],t[-1]

Because () both can represent a tuple and can be used as parentheses to indicate the priority of the operation, it is stipulated that the element tuple should have a comma ","

The so-called "invariant" of a tuple is that each element of a tuple, pointing to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!

Three: Dict dictionary

Curly braces {} indicate that this is a dict,d = { key:value }

Features of Dict :

The first is quick to find (the disadvantage of dict is that it takes up a lot of memory, and it wastes lots of content)

The second is that the stored key-value sequence pairs are not sequential (in addition, key cannot be duplicated)

The third is that the element as key must be immutable (not a list object)

Find value:

D[key] Find the corresponding value, if it does not exist, then the error keyerror

Determine if key exists:

(1) If key in D, returns false if key does not exist

(2) D.get (key), if key does not exist, returns none

Update Dict:

D[key]=value If key already exists, it will replace the original value with the new value

Traverse Dict:

For key in D

Four: Set Set

Set elements are not duplicated and unordered, passing in the same element, set will automatically remove duplicate elements

Characteristics:

Storing elements as immutable objects

Determines whether an element is fast in set.

The set stored elements are also in no order.

Traverse set:

s = set ([' xxx ', ' yyy ', ' zzz ']) for XXX in S

Add se element t:

S.add (PPP), when adding an existing element, will not error, but will not be added in

To delete a set element:

S.remove (zzz), delete element does not exist, will error (before deleting the operation to determine whether the existence, in)

Python_list,tuple,dict,set

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.