Conversion of dictionary lists in Python and the for loop method

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.