码迷,mamicode.com
首页 > 其他好文 > 详细

tmp 字典合并

时间:2019-11-23 21:35:53      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:func   apple   app   ted   ==   ace   nod   pass   ++   

 

s = "‘A’: 1, ‘B.A’: 2, ‘B.B’: 3, ‘CC.D.E’: 4, ‘CC.D.F’: 5";
String.prototype.replaceAll = function(f, e) { //f替换成e
var reg = new RegExp(f, "g"); //创建正则RegExp对象
return this.replace(reg, e);
};
l = s.replaceAll(/ /g, ‘‘).replaceAll(‘‘‘, ‘‘).replaceAll(‘’‘, ‘‘).split(‘,‘);
ret = {};
path=[];
for (i of l) {
ll = i.split(‘:‘);
p={};
if (ll[0].includes(‘.‘)) {
child={};
thisChild={};
ks = ll[0].split(‘.‘);
v = ll[1];
k=ks[ks.length-1];
child[k]=v;
for (ii = ks.length - 1 - 1; ii > 0; ii--) {
k=ks[ii];
thisChild[k]=child;
child = thisChild;
};

p[ks[0]] = child;



} else {
p[ll[0]] = ll[1];
}
path.push(p);

};ret;path;

 

s = "‘A’: 1, ‘B.A’: 2, ‘B.B’: 3, ‘CC.D.E’: 4, ‘CC.D.F’: 5";
String.prototype.replaceAll = function(f, e) { //f替换成e
var reg = new RegExp(f, "g"); //创建正则RegExp对象
return this.replace(reg, e);
};
l = s.replaceAll(/ /g, ‘‘).replaceAll(‘‘‘, ‘‘).replaceAll(‘’‘, ‘‘).split(‘,‘);
ret = {};
list=[]
for (i of l) {
ll = i.split(‘:‘);
if (ll[0].includes(‘.‘)) {
child={};
thisChild={};
ks = ll[0].split(‘.‘);
v = ll[1];
k=ks[ks.length-1];
child[k]=v;
for (ii = ks.length - 1 - 1; ii > 0; ii--) {
k=ks[ii];
thisChild[k]=child;
child = thisChild;
};
if(ks[0] in ret){

}else{
ret[ks[0]] = child;
list.push
}
} else {
ret[ll[0]] = ll[1];
}
};ret;

 

 

 

#include <stdio.h>
int main()
{
    int arr[] = {7};
    int len = sizeof(arr) / sizeof(int);
    int pl[len];
    int i;
    int lt = 0;
    int rt = len - 1;
    for (i = 0; i < len; i++)
    {
        int tmp = arr[i];
        if (tmp > 0)
        {
            pl[lt] = tmp;
            lt += 1;
        }
        else
        {
            pl[rt] = tmp;
            rt -= 1;
        }
    }
    return 0;
}
 
 
inpt = {‘a.v‘: 5, ‘a.b‘: 44, ‘ac‘: 4, ‘a.bf.r.t‘: 44}
# 逐个合并前的dic
dics = []
# 原始key
ks0 = inpt.keys()
# 遍历
for i in ks0:
print(i)
child = {}
# 拆分key
ks, v = i.split(‘.‘), inpt[i]
k = ks[- 1]
child[k] = v
for ii in range(-2, -len(ks) - 1, -1):
# 从内向外建立dic
print(ii)
thisChild = {}
k = ks[ii]
thisChild[k] = child
child = thisChild
dics.append(child)


# 合并2个dic
def merageDics(di, dj):
pass


di, dj = {‘a‘: {‘b‘: 34}}, {‘f‘: 454}
ret = {}

ksAnd = set(list(di.keys())) & set(list(dj.keys()))
ksUnique = set(list(di.keys())) ^ set(list(dj.keys()))

for k in ksUnique:
if k in di:
ret[k] = di[k]
else:
ret[k] = dj[k]
d=di
while len(d.keys())>0:
print(d)
d=d[list(d.keys())[0]]
dd = 9




 

class Node:
    def __init__(self, level=0, p=None, v=None):
        self.level = level
        self.p = p
        self.v = v
root = Node(level=0, p=None, v=None)
tail = Node(level=1, p=None, v=‘a‘)
tail = Node(level=1, p=None, v=‘b‘)




# 合并2个dic
def merageDics(di, dj):
    pass


di, dj = {‘a‘: {‘b‘: 34}}, {‘f‘: 454}
ret = {}

ksAnd = set(list(di.keys())) & set(list(dj.keys()))
ksUnique = set(list(di.keys())) ^ set(list(dj.keys()))

for k in ksUnique:
    if k in di:
        ret[k] = di[k]
    else:
        ret[k] = dj[k]
d=di
while len(d.keys())>0:
    print(d)
    d=d[list(d.keys())[0]]
dd = 9




import copy

d = {‘k‘: ‘kv‘}
j_sc = {‘k1‘: copy.copy(d)}
j_dc = {‘k1‘: copy.deepcopy(d)}
jc = {‘k1‘: d.copy()}
k = {‘k1‘: d}
d = {‘j‘: ‘jv‘}

dd = 9


 
 
 

inpt = {‘a1.v‘: 5, ‘a2.b1‘: 44, ‘a2.b2‘: 424, ‘c1.cc.e‘: 3, ‘c1.cc.f‘: 33, ‘ac‘: 6, ‘a.bf.r.t‘: 9}
# 逐个合并前的dic
dics = []


# key不会做为其他key的前缀
# e:{‘a‘:12,‘a.b‘:34}
# 但允许
# e:{‘a.b1‘:12,‘a.b2‘:34}

def f():
    # 原始key
    ks0 = inpt.keys()
    # 遍历
    for i in ks0:
        child = {}
        # 拆分key
        ks, v = i.split(‘.‘), inpt[i]
        k = ks[- 1]
        child[k] = v
        for ii in range(-2, -len(ks) - 1, -1):
            # 从内向外建立dic
            thisChild = {}
            k = ks[ii]
            thisChild[k] = child
            child = thisChild
        dics.append(child)


def getDepth(d):
    r = 0
    while type(d) == dict:
        for k in d.keys():
            if k is None:
                break
            r += 1
            d = d[k]
    return r


f()


def getNestedDic(keys, tail=None):
    len_, ret = len(keys), tail
    for i in range(-1, -len_ - 1, -1):
        ret = {keys[i]: ret}
    return ret


def getNestedDicTail(dic, keys):
    if not type(dic) == dict:
        return dic
    return dic[keys[-1]]


def getDicChild(d, keys, tail=None):
    ‘‘‘
   
key链路下的dic
    {‘a‘:{‘b‘:{‘c‘:4}}},[‘a‘,‘r‘]
    <class ‘dict‘>: {‘b‘: {‘c‘: 4}, ‘r‘: {}}
    {‘a‘:{‘b‘:{‘c‘:4}}},[‘a‘]
    <class ‘dict‘>: {‘b‘: {‘c‘: 4}}
    {‘a‘:{‘b‘:{‘c‘:4},‘b2‘:{‘c2‘:44}}},[‘a‘,‘r‘]
    <class ‘dict‘>: {‘b‘: {‘c‘: 4}, ‘b2‘: {‘c2‘: 44}, ‘r‘: {}}
    {‘b‘:4}, [‘b1‘,‘gg‘]
    <class ‘dict‘>: {‘b‘: 4, ‘b1‘: {‘gg‘: None}}
   
:param d:
   
:param keys:
   
:return:
    ‘‘‘
   
r = d
    len_ = len(keys)
    r_same = {}
    for i in range(0, len_, 1):
        k = keys[i]
        # 关键代码

        if k in r:
            # 从入口修改
            if i == 0:
                pass
            # {‘a‘:{‘b‘:12}}
            # {‘a‘:{‘c‘:34}} 如何合并
            r = r[k]
            # if type(r[k]) == dict:
            #     r = r[k]
            # else:
            #     pass
            # pass
        else:
            r[k] = getNestedDic(keys[i + 1:], tail=tail)
            r = r[k]
            break
    return r


def getKeys(d):
    """
    keys
单链路
    {‘a‘:{‘b‘:12}} yes
    {‘a‘:12,‘b‘:34} not
   
:param d:
   
:return:
    """
   
ret = []
    while type(d) == dict:
        for k in d.keys():
            ret.append(k)
    return ret

def mergeDics(d0, d1):
    ‘‘‘
    keys
单链路
    {‘a‘:{‘b‘:{‘c‘:12}}}
    {‘a‘:{‘b1‘:34}}
    {‘a‘:{‘b‘:{‘c‘:12},‘b1‘:34}}
   
:param d0:
   
:param d1:
   
:return:
    ‘‘‘
   
d0Dep, d1Dep = getDepth(d0), getDepth(d1)
    if d0Dep > d1Dep:
        b, s = d0Dep, d1Dep
    else:
        s, b = d0Dep, d1Dep
    r=b
    b_keys,s_keys=getKeys(b),getKeys(s)
    b_len,s_len= len(b_keys),len(s_keys)
    for i in range(0, b_len, 1):
        if i+1<s_len:
            continue
        elif i+1==s_len:
            pass



# 怎么更新一个dic?
# 怎么更新一个dic的某个key链路?
#
‘‘‘
dictionary1={‘level1‘:{‘level2‘:{‘levelA‘:0,‘levelB‘:1}},‘anotherLevel1‘:{‘anotherLevel2‘:{‘anotherLevelA‘:0,‘anotherLevelB‘:1}}}
update={‘anotherLevel1‘:{‘anotherLevel2‘:1014}}
dictionary1.update(update)
print dictionary1
{‘level1‘: {‘level2‘: {‘levelB‘: 10}},‘anotherLevel1‘:{‘anotherLevel2‘:1014}}

‘‘‘

import copy

ret = {}
for i in dics:
    print(i)
    ii = copy.deepcopy(i)
    depth = getDepth(ii)
    keys = []
    for _ in range(0, depth, 1):
        for k in ii:
            keys.append(k)
            ii = ii[k]
    # 此处已经实现了更新
    d = getDicChild(ret, keys, tail=getNestedDicTail(ii, keys))
    print(f‘keys {keys[0]}‘)

    ret[keys[0]] = d
    dd = 9

import pprint

pprint.pprint(inpt)
pprint.pprint(ret)
s = 0
# while True and 4 > 6:
#     rank_num = 0
#     ret = {}
#     for i in dics:
#         depth = getDepth(i)
#         keys = []
#         for _ in range(0, depth, 1):
#             for k in i:
#                 keys.append(k)
#         d = getDicChild(ret, keys)
#         ret[keys[0]] = d

# ret[k]=0
#
# for k in i:
#     if k not in ret:
#         ret[k] = {}
#         idx = dics.index(i)
#         if type(i[k]) == dict:
#             dics[idx] = i[k]
#         else:
#             dics[idx] = None

# d = 9


dd = 0

 
 

tmp 字典合并

标签:func   apple   app   ted   ==   ace   nod   pass   ++   

原文地址:https://www.cnblogs.com/yuanjiangw/p/11909669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!