This article introduces the conversion of the dictionary list in Python and the difference in the for loop method.
# Original dictold_dict = {"#1": {'hostname': c1, 'CPU _ count': 2, 'mem _ capicity ': 80}, "#2 ": {'hostname': c1, 'CPU _ count': 2, 'mem _ capicity ': 80} "#3": {'hostname': c1, 'CPU _ count': 2, 'mem _ capicity ': 80 }}# new data new_dict = {"#1": {'hostname': c1, 'CPU _ count': 2, 'mem _ capicity ': 800}, "#3": {'hostname': c1, 'CPU _ count': 2, 'mem _ capicity ': 80} "#4": {'hostname': c2, 'CPU _ count': 2, 'mem _ capicity': 80} required Delete :? Need to create :? Update Required :?
Code:
#-*-Coding: UTF-8 -*-
# Original data
Old_dict = {"#1": {'hostname': 'C1', 'CPU _ count': 2, 'mem _ capicity ': 80}, "#2 ": {'hostname': 'C1', 'CPU _ count': 2, 'mem _ capicity ': 80}, "#3": {'hostname': 'C1 ', 'CPU _ count': 2, 'mem _ capicity ': 80 }}# new data new_dict = {"#1": {'hostname': 'C1 ', 'CPU _ count': 2, 'mem _ capicity ': 800}, "#3": {'hostname': 'C1', 'CPU _ count': 2, 'mem _ capicity ': 80}, "#4": {'hostname': 'C2', 'CPU _ count': 2, 'mem _ capicity ': 80 } Old_set = set (old_dict.keys () update_list = list (old_set.intersection (new_dict.keys () for I in update_list: if cmp (old_dict [I], new_dict [I])! = 0: update_list.remove (I) # print update_list new_list = [] del_list = [] for I in new_dict.keys (): if I not in update_list: new_list.append (I) for I in old_dict.keys (): if I not in update_list: del_list.append (I) print update_listprint new_listprint del_list running result: ['# 3'] [' #1 ', '# 4'] [' # 2', '# 1'] This section uses dictionary and list conversion to remove a value from the list, for loop, cmp built-in function, if judgment
The preceding sections describe how to convert the dictionary list of Python and the for loop method. for more information, see other related articles in the first PHP community!