Python's dict,set,list,tuple application detailed _python

Source: Internet
Author: User
Tags in python

This paper deeply analyzes the application of dict,set,list,tuple in Python and its corresponding example, which helps readers to master the concept and principle. Specifically as follows:

1. Dictionary (dict)

Dict surrounded by {}
Dict.keys (), Dict.values (), Dict.items ()
Hash (obj) returns the hashed value of obj if the return represents 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 the non-existent key,dict.[]
Dict.setdefault () checks whether the dictionary contains a key. If this key exists in the dictionary, you can take it to 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:

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

Do not allow a key to correspond to multiple values
The key value must be hashed, with the hash () test
An object, if the implementation _hash () _ method can be used as a key value

2. Collection (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 difference is that if the deleted element is not in the collection, discard does not complain, remove error
< <= representation subset,> >= representation superset
| Represents union & Representation intersection-representation difference set ^ difference set

3. Listing (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 in the list is variable, we can add, modify, delete the data in the list through the object method. You can convert a sequence type into a list by using the list (SEQ) function.

Append (x) appends a single object x to the tail of the list. Using multiple parameters can cause an exception.
COUNT (x) returns the number of times the object x appears in the list.
Extend (l) adds the table items in list L to the list. Returns none.
Index (x) returns the indexes of the first list item in the list that matches the object X. An exception occurs when there is no matching element.
Insert (I,X) Inserts an object x before an 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 that is indexed as X and returns the value of the table entry. If no index is specified, the pop returns the last item in the list.
Remove (x) deletes the first element of the matching object x in the list. An exception was generated when the element was matched. Returns none.
Reverse () Reverses the order of the list elements.
Sort () Sorts the list, returning none. The Bisect module can be used to add and delete sorted list items.

4. Meta Group (tuple)

tuple= (1,), which is the tuple representation of a single element, requires an extra comma.
tuple=1,2,3,4, which can also be a tuple, allows Python to not use parentheses when it does not use parentheses without causing confusion.
As with lists, you can index, fragment, connect, and repeat tuples. You can also use Len () to find the tuple length.
The index of the tuple is in the form of tuple[i] instead of the tuple (i).
Similar to the list, use tuple (SEQ) to convert other sequence types to infinitesimal 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.