List, tuples, and dictionary functions in python, and python Functions
I. list)
Common features:
1.Append: Append after the list
For example:
>>> A = []
>>> A. append (1)
>>>
[1]
2.Clear: Clear all content in the list
For example:
>>> A = [1]
>>> A. clear ()
>>>
[]
3.Copy: Copy the elements in the list
4.Count: Count the number of elements in the list.
For example:
>>> A = [1, 1]
>>> A. count (1)
2
5.Extend: Merge two lists
For example:
>>> A = [1]
>>> B = [2, 3]
>>> A. extend (B)
>>>
[1, 2, 3]
6.Index: Gets the base value of an element in the list.
For example:
>>> A = [1, 2, 3]
>>> A. index (2)
1
7.Insert: Insert an element to the list.
For example:
>>> A = [1, 2, 3]
>>> A. insert (2, 4)
>>>
[1, 2, 4, 3]
8.Pop: Delete the last element in the list and prompt the element content.
For example:
>>> A = [1, 2, 3]
>>> A. pop ()
3
>>>
[1, 2]
9.Remove: Deletes an element from the list.
For example:
>>> A = [1, 2, 3]
>>> A. remove (1)
>>>
[2, 3]
10.Reverse: Elements in the image list
For example:
>>> A = [1, 2, 4, 3]
>>> A. reverse ()
>>>
[3, 4, 2, 1]
11.Sort: Sorting list elements
For example:
>>> A = [1, 2, 4, 3]
>>> A. sort ()
>>>
[1, 2, 3, 4]
Ii. tuple)
Common functions:
1.Count: Count the number of elements in the list.
For example:
>>> A = (1, 1)
>>> A. count (1)
2
2.Index: Gets the base value of an element in the list.
For example:
>>> A = (1, 2, 3)
>>> A. index (2)
1
3. Dictionary (dict ):
Common functions:
1.Clear: Clear all elements in the dictionary
For example:
>>> A = {"key1": 1}
>>> A. clear ()
>>>
{}
2.Copy: Shortest copy
3.Fromkeys: Create a new dictionary by setting the sequence key and value to value.
For example:
>>> A = {"key1": 1, "key2": 2}
>>> B = a. fromkeys (["key1", "key2", "key3"], 10)
>>> B
{'Key2': 10, 'key3': 10, 'key1': 10}
4.Get: If the key exists in the dictionary, obtain the corresponding value. If the key does not exist in the dictionary, none is returned by default. You can also set a default return value.
For example:
(1) key value in the dictionary:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. get ("key1 ")
1
(2) The key does not exist in the dictionary:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> Print (a. get ("key4 "))
None
>>> Print (a. get ("key4", "nonexistent "))
Does not exist
5.Items: Get the key-value pairs in the dictionary and put them in the list. The key-value pairs are used as the elements in the list as tuples.
For example:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. items ()
Dict_items ([('key2', 2), ('key1', 1), ('key3', 3)])
6.Keys: Get the keys in the dictionary
For example:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. keys ()
Dict_keys (['key2', 'key1', 'key3'])
7.Pop: Delete the key-value pair of the key in the dictionary.
For example:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. pop ("key1 ")
1
>>>
{'Key2': 2, 'key3': 3}
8.Popitem: Randomly Delete the elements in the dictionary
For example:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. popitem ()
('Key2', 2)
>>>
{'Key1': 1, 'key3': 3}
9.Setdefault: Set the initial value corresponding to the dictionary key. If all the key values exist, the original initial value is displayed. If not, none is displayed by default. If not, an initial value is set.
For example:
(1) Key-value pairs already exist:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. setdefault ("key1 ")
1
(2) There is no key-Value Pair:
① Do not enter the default value:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. setdefault ("key4 ")
>>>
{'Key2': 2, 'key4': None, 'key3': 3, 'key1': 1}
② Enter the default value:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. setdefault ("key4", 4)
4
>>>
{'Key2': 2, 'key4': 4, 'key3': 3, 'key1': 1}
10.Update: Update the key-value pairs in the second dictionary to the first dictionary.
For example:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> B = {"key4": 4, "key5": 5}
>>> A. update (B)
>>>
{'Key2': 2, 'key4': 4, 'key3': 3, 'key1': 1, 'key5': 5}
11.Values: Get the dictionary Value
For example:
>>> A = {"key1": 1, "key2": 2, "key3": 3}
>>> A. values ()
Dict_values ([3, 2, 1])
3. Note:
1. The elements in the list can be modified, deleted, and added. You can traverse the elements in the list cyclically. You can use the tuple () function to convert the elements before the tuples.
2. The elements in the tuples cannot be modified, deleted, or added. The elements in the list can be read cyclically and converted between the list function and the list.
3. The value of the element in the dictionary can be modified by using a key. The key cannot be modified.