Python 程式用於統計反向字串對
當需要統計反向字串對時,使用簡單迭代。
示例
下面演示了這一點
my_list = [{"Python": 8, "is": 1, "fun": 9}, {"Python": 2, "is": 9, "fun": 1}, {"Python": 5, "is": 10,"fun": 7}]
print("The list is :")
print(my_list)
result = {}
for dic in my_list:
for key, value in dic.items():
if key in result:
result[key] = max(result[key], value)
else:
result[key] = value
print("The result is :")
print(result)輸出
The list is :
[{'Python': 8, 'is': 1, 'fun': 9}, {'Python': 2, 'is': 9, 'fun': 1}, {'Python': 5, 'is': 10, 'fun': 7}]
The result is :
{'Python': 8, 'is': 10, 'fun': 9}說明
定義了一個字典列表,並將其顯示在控制檯上。
建立一個空字典。
迭代列表的元素。
迭代字典的項。
如果字典中存在鍵,則將鍵和值的較大值分配給結果。
否則,將該值放入結果中。
這是顯示在控制檯上的結果。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP