Python程式比較兩個字典中的元素

王林
發布: 2023-08-20 19:25:06
轉載
1560 人瀏覽過

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 and unique. The name of keys in a dictionary is case sensitive and unique. The name of keys in a dictionary is case sensitive and unique. The name of keys in a dictionary is case sensitive。 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 throughone the comparei one the pare ifle 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 differ methods to compare 2#.

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 pair of both the dictionaries. In the final method we used the list compresion inpyed the list over key value pair of one dictionary and check the values for the keys in both dictionaries and compared them.

The time complexity of 1

st

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 diction have time complexity of O (n). where n is the length of dictionary.

以上是Python程式比較兩個字典中的元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!