Details about python dict, set, list, And tuple applications

Source: Internet
Author: User
This article mainly introduces the application details of python dict, set, list, And tuple. For more information, see this article, the tuple application and corresponding examples help readers understand its concepts and principles. The details are as follows:

1. Dictionary (dict)

Dict surrounded {}
Dict. keys (), dict. values (), dict. items ()
Hash (obj) returns the hash value of obj. If it is returned, it can be used as the key of dict.
Del or dict. pop can delete an item and clear all content.
Sorted (dict) can sort dict
Dict. get () can search for keys that do not exist. dict. [] cannot.
Dict. setdefault () checks whether the dictionary contains a key. If this key exists in the dictionary, you can obtain its value. If the specified key does not exist in the dictionary, you can assign a default value to the key and return this value.
{}. Fromkeys () to create a dict, for example:

{}.fromkeys(('love', 'honor'), True) =>{'love': True, 'honor': True} 

One key cannot correspond to multiple values.
The key value must be hashed and tested using hash ().
An object. If the implement _ hash () _ method can be used as a key value

2. set)

A set is a mathematical concept created using set ().

Set. add (), set. update. set. remove, add update and delete,-= can be used for set subtraction.
The difference between set. discard and set. remove is that if the deleted element is not in the set, discard does not report an error and remove returns an error.
<= Indicates a subset,> = indicates a superset.
| Indicates Union & indicates intersection-indicates difference set ^ difference diversity

3. list)

A list is a sequence object that can contain any Python data information, such as strings, numbers, lists, and tuples. The data in the list is variable. You can use the object method to add, modify, or delete data in the list. You can use the list (seq) function to convert a sequence type into a list.

Append (x) append a single object x at the end of the list. Multiple parameters may cause an exception.
Count (x) returns the number of times object x appears in the list.
Extend (L) adds the table items in list L to the list. Return None.
Index (x) returns the Index of the first list item that matches object x in the list. An exception occurs when no matching element exists.
Insert (I, x) inserts object x before the element whose index is I. For example, list. insert (0, x) inserts an object before the first entry. Return None.
Pop (x) deletes the table item indexed as x in the list and returns the value of this table item. If no index is specified, pop returns the last item in the list.
Remove (x) deletes the first element in the list that matches object x. An exception occurs when an element is matched. Return None.
The order of list elements is reversed by reverse.
Sort () sorts the list and returns none. The bisect module can be used to add or delete sort list items.

4. tuple)

Tuple = (1,), which indicates the tuples of a single element. An additional comma is required.
Tuple = 1, 2, 3, 4. This can also be a tuples. Python allows not to use the tuples of parentheses when parentheses are not used and will not cause confusion.
Like the list, you can index, partition, join, and repeat tuples. You can also use len () to evaluate the length of the tuples.
The index of tuples is in the form of tuple [I] instead of tuple (I ).
Similar to the list, you can use tuple (seq) to convert other sequence types into tuples.

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.