This article example describes the Python implementation of finding the same point of two dictionaries. Shared for your reference, as follows:
Question:Look for the same place in the middle of two dictionaries (same key, same value, etc.)
Solution:pass (a bill or inspection etc)keys()
oritems()
methods to perform common set operations (such as finding the union, intersection, and difference)
>>> a={'x':1,'y':2,'z':3} >>> b={'ww':10,'x':11,'y':2} >>> ()& () # Intersection of keys {'y', 'x'} >>> ()- () # Difference sets of keys {'z'} >>> ()| () # Keyed Concatenation {'ww', 'y', 'x', 'z'} >>> ()& () {('y', 2)} >>> ()- () {('z', 3), ('x', 1)} >>> ()| () {('ww', 10), ('z', 3), ('x', 1), ('x', 11), ('y', 2)} >>>
These types of operations can also be used to modify or filter out the contents of a dictionary. Example:
>>> c = {key:a[key] for key in ()-{'w','z'}} # Create a new dictionary with some of the keys removed. >>> c {'y': 2, 'x': 1} >>>
Summary:
dictionarykeys()
Methods,items()
method supports collection operations, but thevalues()
method is not supported. This is because it is not guaranteed that all values in a dictionary are unique from the point of view of the value, which can lead to problems with certain set operations. However, such operations can be realized by converting values to collections.
(Code taken from thePython Cookbook》)
Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.