20th, basic data type, set set, comprehensive application new data update old data

Source: Internet
Author: User
Tags set set

Basic data type, set set, comprehensive application new data update old data

Create a two dictionary
New data, update the original data, A is the original data, B is the new data

1, respectively get to the A dictionary and the B Dictionary key (key), the two dictionary keys are converted to two sets

2, find the existence of the A2 collection, the B2 collection does not exist in the element, get: {' #2 ', ' #3 '} (need to delete:? )

3, find the element that exists in the B2 collection, a2 not exist in the collection, get: {' #4 ', ' #7 '} (need to create new:? )

4, find the elements of the A2 collection and the B2 set that have the intersection, that is, the A2 collection and the B2 set of elements that exist, get: {' #1 '} (need to update:? )

5, in the original data (the original data, there is no new data deleted)

6, in the original data (some new data, the original data is not appended)

7, in the original data (both the original data and the new data, the value of the new data is updated into the original data)

#!/usr/bin/env python#-*-coding:utf-8-*-#Create a two dictionary#new data, update the original data, A is the original data, B is the new dataA = {    "#1": 11,    "#2": 22,    "#3": 100}Print("Raw Data", a) b= {    "#1": 33,    "#4": 22,    "#7": 100}Print("the new data", B)#Take out the key (key) in the A dictionary and get: Dict_keys ([' #1 ', ' #2 ', ' #3 '])A1 =A.keys ()#converts the key obtained by A1 into a collection, resulting in: {' #3 ', ' #2 ', ' #1 '}A2 =set (A1)#Take out the key (key) in the B dictionary and get: Dict_keys ([' #4 ', ' #1 ', ' #7 '])B1 =B.keys ()#converts the key obtained by B1 into a collection, resulting in: {' #7 ', ' #4 ', ' #1 '}B2 =Set (B1)#find the elements in the A2 collection that do not exist in the B2 collection, and get: {' #2 ', ' #3 '} (need to delete:?). )A3 =a2.difference (B2)#find the elements in the B2 collection that do not exist in the A2 collection, and get: {' #4 ', ' #7 '} (need to create a new:? )B3 =b2.difference (A2)#Find the elements of the intersection of the A2 collection and the B2 collection, that is, the elements that exist in the A2 collection and the B2 collection, and get: {' #1 '} (need to update:?). )c =a2.intersection (B2)#Delete the original data, you need to delete the#A3 in the collection, it needs to be deleted. forIinchA3:#A3 The key of a dictionary every time the collection is looped, delete this key element    delA[i]Print("\ n")Print("Delete the original data, the new data does not exist", a)#Create new raw data, you need to create a new#B3 in the collection, we need a new one . forIinchB3:#re-assign to a variable that loops through the B3 collectionJ1 =I#The key for the B dictionary, which is the loop in the B3 collection, gets the value corresponding to this key, re-assigns the value to a variableJ2 =B[i]#append a new element to the A dictionaryA[J1] =J2Print("new data inside, not in the original data, added to the original data", a)#update the original data, need to update the#In the C collection, it needs to be updated. forIinchc:g=I a[g]=B[g]Print("in the original data, there are also new data, update the new data to the original data"A

20th, basic data type, set set, comprehensive application new data update old data

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.