Created
June 20, 2017 13:24
-
-
Save recuraki/37de9388722e668997c90bb8f154caea to your computer and use it in GitHub Desktop.
dictはsetで引き算できないので、一回、tupleにしてからsetで演算してdictに戻すテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dictはsetで引き算できないので、一回、tupleにしてからsetで演算してdictに戻すテスト | |
before = [{'netmask': '32', 'ipAddr': '192.168.0.1'}, {'netmask': '32', 'ipAddr': '213.199.128.119'}] | |
after = [{'netmask': '32', 'ipAddr': '192.168.0.1'}, ] | |
print("before: {0}".format(before)) | |
print("after: {0}".format(after)) | |
#before: [{'netmask': '32', 'ipAddr': '192.168.0.1'}, {'netmask': '32', 'ipAddr': '213.199.128.119'}] | |
#after: [{'netmask': '32', 'ipAddr': '192.168.0.1'}] | |
addrList2addrTuple = lambda x: (x["netmask"], x["ipAddr"]) | |
beforeTuple = list(map(addrList2addrTuple, before)) | |
afterTuple = list(map(addrList2addrTuple, after)) | |
print("beforeTuple: {0}".format(beforeTuple)) | |
print("afterTuple: {0}".format(afterTuple)) | |
# beforeTuple: [('32', '192.168.0.1'), ('32', '213.199.128.119')] | |
# afterTuple: [('32', '192.168.0.1')] | |
result = set(beforeTuple) - set(afterTuple) | |
print("beforeTuple - afterTuple(set): {0}".format(result)) | |
#beforeTuple - afterTuple(set): {('32', '213.199.128.119')} | |
addrTuple2addrList = lambda x: {"netmask": x[0], "ipAddr": x[1]} | |
resultDict = list(map(addrTuple2addrList, result)) | |
print("beforeTuple - afterTuple(dict): {0}".format(resultDict)) | |
# beforeTuple - afterTuple(dict): [{'netmask': '32', 'ipAddr': '213.199.128.119'}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment