卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

Python程序比较两个字典中的元素

Python程序比较两个字典中的元素

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中的字典

在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.

Example

在下面的示例中,我们使用==运算符来比较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 ")

Output

The output for the above code will be –

dict1 is not equal to dict2

使用循环比较两个字典

在这种方法中,我们将逐个比较两个字典的元素,通过迭代一个字典的长度,并在每次迭代中检查对应字典中的键和值与其他字典中的相应键和值对进行比较。

我们还将检查两个字典的长度,如果它们不相同,我们可以直接得出结论,这两个字典不相等。要获取字典中与键对应的值,我们使用. get函数,该函数给出作为参数的键的值。

Example

在下面的例子中,我们将会。

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 ")

Output

上述程序的输出如下:

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.

Example

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")

Output

上述代码的输出结果如下:

dict1 and dict2 are not equal

Conclusion

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.

卓越飞翔博客
上一篇: 在PHP中编写一个程序,用于查找一个字符在字符串中出现的次数
下一篇: 如何在Python中获取整数字面量属性而不是SyntaxError?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏