简体   繁体   English

python:检查一个键中的一定数量的字符是否与同一个字典中另一个键的字符匹配

[英]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: 例如:

  • the first key ' M.ESE18.COFMSDM01V .MW.IT' would be compared with all the other keys in the initial dictionary 第一个键' M.ESE18.COFMSDM01V .MW.IT'将与初始字典中的所有其他键进行比较
  • because its first 18 characters are identical in the case of 2 other keys in the dictionary (' M.ESE18.COFMSDM01V .MN.ML',' M.ESE18.COFMSDM01V .CI.CR'), then 因为它的前18个字符在字典中的2个其他键的情况下是相同的(' M.ESE18.COFMSDM01V .MN.ML',' M.ESE18.COFMSDM01V .CI.CR'),那么
  • M.ESE18.COFMSDM01V (the common part of the 3 keys) will become a new "top-level" key in the new dict, with the matching 2 keys now turned into it's value as nested dictionaries M.ESE18.COFMSDM01V (3个键的常用部分)将成为新词典中的一个新的“顶级”键,匹配的2个键现在变成了它作为嵌套词典的价值

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: 提及:

  • There will be multiple cases when several of the initial keys will have common characters and therefore would need to be included in a new top-level key, but i won't know which or how many 当多个初始密钥具有共同字符时会出现多种情况,因此需要将其包含在新的顶级密钥中,但我不知道哪个或多少个
  • Some keys in the dictionary will be completely unique (in terms of their first 18 chars), but i still need to create the same structure even for those in the new dictionary 字典中的一些键将是完全唯一的(就前18个字符而言),但我仍然需要为新字典中的那些创建相同的结构

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM