Python dict set list tuple application details

  • 2020-04-02 13:52:36
  • OfStack

This article makes an in-depth analysis of dict,set,list and tuple applications and corresponding examples in python, which will help readers to grasp their concepts and principles. The details are as follows:

1. The dictionary (dict)

Dict is surrounded by {}
Dict. Keys (), dict. Values (), dict. The items ()
Hash (obj) returns the hash value of obj, which if returned can be used as the key of dict
Del or dict.pop can delete an item and clear all the contents
I can sort the dict
Dict. get() can find the key that does not exist, dict.[] cannot
Dict. setdefault() checks if the dictionary contains a key. If the key exists in the dictionary, you can take its value. If the key does not exist in the dictionary, you can assign a default value to the key and return the value.
{}. Fromkeys () creates a dict, for example:


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

One key is not allowed to correspond to more than one value
Key values must be hashed, tested with hash()
An object that can be used as a key if the _hash()_ method is implemented

2. Set (set)

A set is a mathematical concept created with set()

Set.add (),set.update.set.remove, add update delete, -= can do set subtraction
The difference between set.delete and set.remove is that if the element is not in the set, it does not report an error
< < = represents the subset, > > = represents the superset
| represents the union & represents the intersection - represents the difference set ^ the difference set

3. List (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 mutable, we can use object method to add, modify, delete and so on the data in the list. The list(seq) function converts a sequence type to a list.

Append (x) appends a single object x to the end of the list. Using more than one parameter causes an exception.
Count (x) returns the number of times the object x appears in the list.
Extend (L) adds table entries from list L to the list. Return None.
Index(x) returns the Index of the first list item in the list that matches object x. An exception occurs when there is no matching element.
Insert (I,x) inserts an object x before an element with index I. For example, list.insert(0,x) inserts an object before the first item. Return None.
Pop (x) drops a table entry indexed x from the list and returns the value of that 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 occurs when an element is matched. Return None.
Reverse () reverses the order of the list elements.
Sort () sorts the list and returns none. The bisect module can be used to sort the addition and deletion of list items.

4. A tuple (a tuple)

Tuple =(1,), which is a tuple representation of a single element, with an extra comma.
Tuple = 1,2,3,4, which can also be a tuple, Python allows tuples without parentheses without causing confusion.
Like lists, tuples can be indexed, shard, joined, and duplicated. You can also use len() to find the tuple length.  
The index of the tuple is in the form of a tuple[I], not a tuple(I).
Like lists, you can use a tuple(seq) to convert other sequence types into tuples.


Related articles: