Following on from the previous article, we will continue to count the methods commonly used for each data type. This article will show three data types: tuple, dictionary, and collection. Hope you can support (like, forward, follow)

Tuple related methods (operations)

There are few ways to ascribe tuples to other types, and most of them are generic, for example:

Access to a tuple
Access an element inside a tuple by subscript
tup = ("python"."java"."go"."php")
print(tup[1])
>>> java
# capture multiple elements by subscript
print(tup[1:2])
>>> ("java"."go")
Copy the code
Tuple built-in functions
The method name describe
cmp(tup1, tup2) Compares two tuple elements
max(tuple) Returns the maximum number of elements in a tuple
min(tuple) Returns the minimum value of an element in a tuple

Dict related methods

Dictionaries are another mutable container model that can store objects of any type (in the form of keys: values)

clear()

Removes all elements from the dictionary

dict = {"1": "python"."2": "java"."3": "go"}
print(dict.clear())
>>> None
Copy the code
copy()

Returns a shallow copy of the original dictionary

dict = {"1": "python"."2": "java"."3": "go"}
dict_copy = dict.copy()
print(dict_copy)
>>> {"1": "python"."2": "java"."3": "go"}
Copy the code
formkeys(可迭代[, value])

Class method that returns a new dictionary

  • Iterable — sequence of dictionary keys
  • Value — Optional argument that sets the value of the key sequence (iterable)
seq = ("python"."java"."go"."php")
dict1 = dict.fromkeys(seq)
dict2 = dict.fromkeys(seq, 1)
print(dict1)
print(dict2)
>>> {'python': None.'java': None.'go': None.'php': None}
>>> {'python': 1.'java': 1.'go': 1.'php': 1}
Copy the code
get(key[, default])

Return the value of key if it exists in the dictionary, default otherwise. If default is not given, it defaults to None, so this method will never raise KeyError

  • Key – The key to look up in the dictionary
  • Default – Returns the default value if the value of the specified key does not exist
dict = {"1": "python"."2": "java"."3": "go"}
print(dict.get("1"))
print(dict.get("0"."Please enter the correct KEY value"))
>>> "python"
>>> "Please enter the correct KEY value"
Copy the code
items()

Returns a traversable (key, value) in a list containing tuples, each of which has a pair of key: values

dict = {"1": "python"."2": "java"."3": "go"}
for key,values in  dict.items():
    print key,values   
>>> 1 python
>>> 2 java
>>> 3 go
Copy the code
keys()

Returns all keys of a dictionary as a list

dict = {"1": "python"."2": "java"."3": "go"}
for key in dict.keys():
	print(key)
>>> 1
>>> 2
>>> 3
Copy the code
pop(key[, default])

Remove key if it exists in the dictionary and return its value, otherwise return default. If default is not given and key does not exist in the dictionary, a KeyError is raised

  • Key — The key to delete from the dictionary
  • Default – Returns the default value if the value of the specified key does not exist
dict = {"1": "python"."2": "java"."3": "go"}
print(d.pop("1"))
print(d.pop("0"."Key value does not exist"))
>>> python
>>> The key value does not existCopy the code
popitem()

Removing a dictionary and returning a (key, value) pair is returned in last-in, first-out order for consumptive iteration of the dictionary, which is often used in set algorithms. If the dictionary is empty, calling popItem () raises KeyError

dict = {"1": "python"."3": "go"}
print(dict.popitem())
print(dict.popitem())
print(dict.popitem())
>>> ('3'.'go')
>>> ('1'.'python')
>>> KeyError: 'popitem(): dictionary is empty'
Copy the code
setdefault(key[, default])

If the dictionary contains the key, return its value. If not, insert key with value default and return default. Default The default is None

  • Key — The key value to find.
  • Default — The default value to set if the key does not exist.
dict = {"1": "python"."3": "go"}
print(dict.setdefault("1".None))
print(dict.setdefault("0"."Test"))
print(dict)
>>> python
>>> test>>> {'1': 'python'.'2': 'java'.'3': 'go'.'0': 'test'}
Copy the code
update([other])

Update the dictionary with key/value pairs from other, overwriting the original keys. Returns None Update () that accepts another dictionary object, or an iterable that contains key/value pairs (represented as tuples of length two or other iterable). If a keyword argument is given, the dictionary is updated with its specified key/value pair: d.update(red=1, blue=2)

  • Other – Key/value pairs added to the dict of the specified dictionary
dict = {"1": "python"."2": "java"."3": "go"}
dict.update({"4": "php"})
dict.update(red=1, blue=2, green=3)
print(dict)
>>> {'1': 'python'.'2': 'java'.'3': 'go'.'4': 'php'.'red': 1.'blue': 2.'green': 3}
Copy the code
values()

Returns all values in the dictionary as a list

dict = {"1": "python"."2": "java"."3": "go"}
print(d.values())
>>> ['python'.'java'.'go']
Copy the code

Set – dependent methods

add(elem)

Add the element ELEm to the collection

  • Elem — The element to be added
set = {"python"."java"}
set.add("go")
set.add("python")		# Existing elements in the collection will not be added (de-duplicated)
print(set)
>>> {'java'.'python'.'go'}
Copy the code
remove(elem)

Removes the element elem from the collection. Raises KeyError if elem does not exist in the collection

  • Elem — The element to be removed
set = {"python"."java"}
set.remove("python")
set.remove("go")
print(set)
>>> {'java'}
>>> KeyError: 'go'
Copy the code
discard(elem)

Remove the element elem if it exists in the collection

  • The element to be removed
set = {"python"."java"}
set.discard("python")
set.discard("go")
print(set)
>>> {'java'}
Copy the code
pop()

Removes and returns any element from the collection. A KeyError is raised if the collection is empty

set = {"python"}
set.pop()
print(set)
>>> set(a)set.pop()
print(set)
>>> KeyError: 'pop from an empty set'
Copy the code
clear()

Removes all elements from the collection.

set = {"python"."java"}
set.clear()
print(set)
>>> set(a)Copy the code
difference(*others)

Returns a new collection containing elements from the original collection that do not exist in other collections specified by Others

set1 = {"python"."java"}
set2 = {"python"."php"."go"}
set3 = set1.difference(set2)
print(set3)
>>> {'java'}
Copy the code
intersection(*others)

Returns a new collection containing elements common to the original collection and all collections specified by Others

  • Othera — A collection of the same elements to look for
set1 = {"python"."java"}
set2 = {"python"."php"."go"}
set3 = set1.intersection(set2)
print(set3)
>>> {'python'}
Copy the code
isdisjoint(other)

Checks whether two collections contain the same element, returning True if none, False otherwise

  • Other — The set to compare
set1 = {"python"."java"}
set2 = {"python"."php"."go"}
set3 = {"php"."go"}
print(set1.isdisjoint(set2))
print(set1.isdisjoint(set3))
>>> False
>>> True
Copy the code
issubset(other)

Determines whether the specified collection is a subset of the method parameter collection

  • Other — the collection to be searched for
set1 = {"python"."java"}
set2 = {"python"."php"."go"."java"}
set3 = {"python"."php"."go"}
print(set1.issubset(set2))
print(set1.issubset(set3))
>>> True
>>> False
Copy the code
issuperset(other)

Determines whether all elements of the specified collection are contained in the original collection, and returns True if so, False otherwise

  • Other — the collection to be searched for
set1 = {"python"."php"."go"."java"}
set2 = {"python"."c++"}
set3 = {"python"."php"."go"}
print(set1.issubset(set2))
print(set1.issubset(set3))
>>> False
>>> True
Copy the code
update(*others)

Update the collection to add all elements from Others

  • Other — Can be an element or a collection
set1 = {"python"."php"."go"."java"}
set2 = {"python"."c++"}
set1.update(set2)
print(set1)
>>> {'c++'.'python'.'java'.'php'.'go'}
Copy the code
union(*others)

Method returns the union of two sets, that is, the set containing all the elements of the set, repeating elements only once

  • Other — The merged target set
set1 = {"python"."php"."go"."java"}
set2 = {"python"."c++"}
set3 = {"python"."php"."go"}
result = set1.union(set2, set3)
print(result)
>>> {'c#'.'python'.'c++'.'java'.'go'.'php'}
Copy the code

At this point, common methods for Python data types have been tallied up.