Dictionaries are a powerful data type in Python that allow you to store data as key-value pairs. In this article, we will be discussing how to compare elements in two dictionaries in Python. We will go over the syntax for comparing dictionary elements and will provide examples of how to do so.
在Python中,可以通过将一系列元素放置在花括号 { } 中,用逗号(,)分隔来创建字典。字典保存键值对,其中一个是键,另一个是对应的值。
Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable and unique. The name of keys in a dictionary is case sensitive. Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly brackets { }.
We can declare a dictionary in the following way −
thisdict = { "brand": "Ford", "model": "Mustang", year": 1964 }
在这篇文章中,我们将会看到如何使用3种不同的方法在Python中比较两个字典的元素。
在这种方法中,我们将使用双等号比较运算符来比较两个字符串。当操作符的左右两边相等时,== 运算符返回 true,当它们不相等时返回 false。
If the 2 dictionaries given to us are equal and identical to each other, this operator will return true and we can conclude that the two dictionaries are equal. And it will return false if they are not equal.
在下面的示例中,我们使用==运算符来比较2个字典
dict1 = { 'first' : 'apple' , 'second' : 'orange' , 'third' : 'mango' } dict2 = { 'first' : 'apple' , 'second' : 'orange' , 'third' : 'grapes'} if dict1 == dict2: print (" dict1 is equal to dict2 ") else: print (" dict1 is not equal to dict2 ")
The output for the above code will be –
dict1 is not equal to dict2
在这种方法中,我们将逐个比较两个字典的元素,通过迭代一个字典的长度,并在每次迭代中检查对应字典中的键和值与其他字典中的相应键和值对进行比较。
我们还将检查两个字典的长度,如果它们不相同,我们可以直接得出结论,这两个字典不相等。要获取字典中与键对应的值,我们使用. get函数,该函数给出作为参数的键的值。
在下面的例子中,我们将会。
dict1 = { 'first' : 'apple' , 'second' : 'orange' , 'third' : 'mango' } dict2 = { 'first' : 'banana' , 'second' : 'guava' , 'third' : 'grapes'} if len (dict1) != len (dict2): print ("The dictionaries are not equal ") else: flag=0 for i in dict1: if dict1.get(i) != dict2.get(i): flag=1 break if flag==0: print (" dict1 is equal to dict2 ") else: print (" dict1 is not equal to dict2 ")
上述程序的输出如下:
dict1 is not equal to dict2
In this method, we will use list comprehension to compare two dictionaries. List comprehension is a shorter way to write a for loop in a list, tuple or dictionary. In this method we will iterate through one of the dictionary and compare if the values for same key in both the dictionaries is same or not. If they are same the dictionaries will be equal and not equal of they are not the same.
The below python code shows how we can use list comprehension to compare two given dictionaries and print the result.
dict1 = { 'first' : 'apple' , 'second' : 'orange' , 'third' : 'mango' } dict2 = { 'first' : 'banana' , 'second' : 'guava' , 'third' : 'grapes' } ans = all ( dict2.get (key) == value for key , value in dict1.items() ) if ans == 'true': print ("dict1 and dict2 are equal") else: print ("dict1 and dict2 are not equal")
上述代码的输出结果如下:
dict1 and dict2 are not equal
In this article, we came to know about dictionaries in python, where we can use dictionaries. We also learnt how we can compare 2 given dictionaries. We came across 3 different methods to compare 2 dictionaries.
The first method involved use of equality operator ( ==). The second method involved use of iteration to check each and every key value pair of both the dictionaries. In the final method we used the list comprehension method of python to iterate over key value pair of one dictionary and check the values for the keys in both dictionaries and compared them.
The time complexity of 1st approach is O (1) as it uses simple comparison. Whereas the other 2 method have time complexity of O (n). where n is the length of dictionary.
以上是Python程序比较两个字典中的元素的详细内容。更多信息请关注PHP中文网其他相关文章!