Last active
July 17, 2019 06:35
-
-
Save alphajc/50ef015fa01ee35c9467c26953439a69 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let obj = { 'A': 1, 'B.A': 2, 'B.B': 3, 'CC.D.E': 4, 'CC.D.F': 5} | |
function split(obj) { | |
for (let [key, value] of Object.entries(obj)) { | |
nks = key.split('.'); | |
if (nks.length > 1) { | |
nk = nks.shift(); | |
obj[nk] = obj[nk] || {}; | |
obj[nk][nks.join('.')] = value; | |
delete obj[key]; | |
split(obj[nk]); | |
} | |
} | |
} | |
split(obj) | |
console.log(obj); |
This file contains hidden or 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 = { 'A': 1, 'B.A': 2, 'B.B': 3, 'CC.D.E': 4, 'CC.D.F': 5} | |
from copy import deepcopy as dc | |
def split(dict): | |
tmp = dc(dict) | |
for k, v in tmp.items(): | |
nks = k.split('.') | |
if len(nks) > 1: | |
nk = nks.pop(0) | |
dict[nk] = dict[nk] if nk in dict else {} | |
dict[nk]['.'.join(nks)] = v | |
del dict[k] | |
split(dict[nk]) | |
split(dict) | |
print(dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment