[英]python: check if a certain number of characters in a key match those of another key in the same dictionary
I have the following dictionary: 我有以下字典:
dic_raw={'M.ESE18.COFMSDM01V.MW.IT':{'initiativeType': 'Activation', 'initiativeName': 'Campaign1'},'M.ESE18.COFMSDM01V.MN.ML':{'initiativeType': 'Revokation', 'initiativeName': 'Campaign 2'},'M.ESE18.COFMSDM01V.CI.CR':{'initiativeType': 'Stratification', 'initiativeName': 'Campaign 3'},'M.ESE18.COFMSDM01A.OP.TA':{'initiativeType': 'Activation', 'initiativeName': 'Campaign 5'}, 'N.ESE18.FFFMSDM01A.NS.TA':{'initiativeType': 'Activation', 'initiativeName': 'Campaign 6'}, 'N.ESE18.FFFMSDM01A.OP.TA':{'initiativeType': 'Activation', 'initiativeName': 'Campaign 4'}}
What i would need is to create another dictionary ( dic_new ), with the same info as dic_raw + another level of depth for the keys. 我需要的是创建另一个字典( dic_new ),使用与dic_raw相同的信息+键的另一个深度级别。 That new level of key in dic_new comes from the first 18 matching characters of any of the keys in the initial dic_raw , and it's value should be a nested dictionary of the "old" 18chars-matching keys + all their "old" values.
dic_new中的新关键字来自初始dic_raw中任意一个键的前18个匹配字符,它的值应该是“旧”18个字符串匹配键+所有“旧”值的嵌套字典。
Like this: 像这样:
dic_new={'M.ESE18.COFMSDM01V': {'M.ESE18.COFMSDM01V.MW.IT': {'initiativeType': 'Activation', 'initiativeName': 'Campaign 1'}, 'M.ESE18.COFMSDM01V.MN.ML': {'initiativeType': 'Revokation', 'initiativeName': 'Campaign 2'}, 'M.ESE18.COFMSDM01V.CI.CR': {'initiativeType': 'Stratification', 'initiativeName': 'Campaign 3'}}, 'N.ESE18.FFFMSDM01A':{'N.ESE18.FFFMSDM01A.NS.TA': {'initiativeType': 'Activation', 'initiativeName': 'Campaign 6'}, 'N.ESE18.FFFMSDM01A.OP.TA': {'initiativeType': 'Activation', 'initiativeName': 'Campaign 4'}}, 'M.ESE18.COFMSDM01A':{'M.ESE18.COFMSDM01A.OP.TA': {'initiativeType': 'Activation', 'initiativeName': 'Campaign 5'}}}
Eg: 例如:
Therefore 因此
M.ESE18.COFMSDM01V.MW.IT:{'initiativeType': 'Activation','initiativeName': 'Campaign 1'}
M.ESE18.COFMSDM01V.MN.ML:{'initiativeType': 'Revokation','initiativeName': 'Campaign 2'}
should become 应该成为
M.ESE18.COFMSDM01V:{'M.ESE18.COFMSDM01V.MW.IT': {'initiativeType': 'Activation', 'initiativeName': 'Campaign 1'},'M.ESE18.COFMSDM01V.MN.ML': {'initiativeType': 'Revokation', 'initiativeName': 'Campaign 2'}}
Mentions: 提及:
I'm very new at programming, so i've tried going about the problem in multiple ways but i'm really not able to find a solution. 我是编程新手,所以我尝试过多种方式解决问题,但我真的找不到解决方案。 I believe i'm unable to break the problem correctly down into the separate pieces (finding the matching keys in a character-by-character indexable way --couldn't figure out how do it in the dict so i needed to move the keys to a list but then i got stuck on how to do the comparison properly, then moving the part i need to a new dict while also copying as nested dicts it's old matching values, etc.)
我相信我无法将问题正确分解为单独的部分(以逐个字符的索引方式找到匹配的键 - 不知道它是如何在dict中这样我需要移动键到一个列表,但后来我卡住了如何正确地进行比较,然后移动我需要一个新的dict的部分,同时也复制作为嵌套的dicts它是旧的匹配值,等等)
Any help would be kindly appreciated! 任何帮助将不胜感激!
A way to make this easily : 一种轻松实现这一目标的方法:
dic_new = {}
for key, value in dic_raw.iteritems():
dic_new.setdefault(key[:18], {}).setdefault(key, value)
Try this: 尝试这个:
dic_new={}
for k in dic_raw:
nk = k[:18]
if not nk in dic_new:
dic_new[nk]={}
dic_new[nk][k] = dic_raw[k]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.