A detailed description of Python's dict,set,list,tuple application

Source: Internet
Author: User
This paper analyzes the application of dict,set,list,tuple in Python and its corresponding examples, which can help readers grasp the concept and principle. Specific as follows:

1. Dictionary (dict)

Dict surrounded by {}
Dict.keys (), Dict.values (), Dict.items ()
Hash (obj) returns the hash value of obj if it returns a key that can be used as a dict
Del or dict.pop can delete a item,clear erase all content
Sorted (dict) can sort the dict
Dict.get () can find a key,dict.[that does not exist] can not
Dict.setdefault () Checks if the dictionary contains a key. If the key exists in the dictionary, you can take its value. If the key you are looking for does not exist in the dictionary, you can assign a default value to the key and return this value.
{}.fromkeys () creates a dict, for example:

One key is not allowed to correspond to multiple values
The key value must be hashed and tested with a hash ()
An object that implements the _hash () _ method can be used as a key value

2. Set (SET)

A collection is a mathematical concept that is created with set ()

Set.add (), set.update.set.remove, add update Delete,-= can do set subtraction
Set.discard and Set.remove is different if the deleted element is not in the collection, discard does not error, remove error
< <= represents a subset,> >= represents a superset
| Represents the Union & representation intersection-Represents the difference set ^ difference set

3. Lists (list)

A list is a sequence object that can contain arbitrary Python data information, such as strings, numbers, lists, tuples, and so on. The data of the list is variable, we can add, modify, delete the data in the list through the object method. You can convert a sequence type to a list by using the list (SEQ) function.

Append (x) appends a single object x to the tail of the list. Using more than one parameter can cause an exception.
COUNT (x) returns the number of times the object x appears in the list.
Extend (l) adds a table entry in the list L to the list. Returns none.
Index (x) returns the indexes of the first list item in a list that matches the object X. An exception occurs when there are no matching elements.
Insert (I,X) Inserts an object x before the element indexed to I. such as List.insert (0,X) Inserts an object before the first item. Returns none.
Pop (x) deletes the table entry in the list indexed to X and returns the value of the table entry. If no index is specified, POP returns the last item in the list.
Remove (x) removes the first element in the list that matches object X. An exception is generated when the element is matched. Returns none.
Reverse () Reverses the order of the list elements.
Sort () Sorts the list and returns none. The Bisect module can be used to sort list item additions and deletions.

4. Tuples (tuple)

tuple= (1,), which is a tuple representation of a single element, requires an extra comma.
tuple=1,2,3,4, this can also be a tuple, which allows a tuple without parentheses to be used without the use of parentheses without causing confusion.
As with lists, tuples can be indexed, fragmented, concatenated, and duplicated. You can also use Len () to find the tuple length.
The tuple's index is in the form of tuple[i], not a tuple (i).
Like lists, use tuple (SEQ) to convert other sequence types to Narimoto groups.

  • 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.