Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从长度为n的数组中生成密钥_Python_Json_List - Fatal编程技术网

Python 如何从长度为n的数组中生成密钥

Python 如何从长度为n的数组中生成密钥,python,json,list,Python,Json,List,我正在尝试用python生成一个字典,该字典稍后将转换为JSON对象 我有一个元素列表['element1','element2',…'elementn'],我想用它作为键 以下是: list1=['thing1','thing2',…'thingn'] 列表2=['element1','element2',…'elementn'] 我想创建以下JSON对象: x = { 'thing1' : { 'thing2' : { ....

我正在尝试用python生成一个字典,该字典稍后将转换为JSON对象

我有一个元素列表['element1','element2',…'elementn'],我想用它作为键

以下是:

list1=['thing1','thing2',…'thingn']
列表2=['element1','element2',…'elementn']
我想创建以下JSON对象:

x = {
    'thing1' : {
        'thing2' : {
            ....
                'thingn': {}
        }
    },
    'element1' : {
        'element2' : {
            ....
                'elementn': {}
        }
    }
}
这样我就可以访问x了:

x['thing1']['thing2]...['thingn']
并添加如下新数据:

x['thing1']['thing2]['newdata']

由于最终的JSON对象将是分层的,数据将在以后添加,因此我希望遍历键列表并生成如上所述的层次结构。

它使用
for
循环获取元素并创建字典,它使用
start
获取嵌套字典以在下一个循环中创建下一个字典

EDIT:我将name
start
更改为
node
,这样可以更好地描述这个变量中的内容。它用于在此结构中进行更深层次的移动,如在树或图形中从一个节点移动到另一个节点

x = {}

list1 = ['thing1','thing2','thingn']
list2 = ['element1','element2','elementn']

node = x  # starting node
for item in list1:
    node[item] = {}    # create new sub-dictionary
    node = node[item]  # move to new sub-dictionary

node = x  # starting node
for item in list2:
    node[item] = {}    # create new sub-dictionary
    node = node[item]  # move to new sub-dictionary

print(x)

print(x['thing1']['thing2']['thingn'])

x['thing1']['thing2']['newdata'] = "Hello World!"
print(x['thing1']['thing2']['newdata'])

编辑:与函数相同

def add(start_node, levels):
    node = start_node      # starting node
    for item in levels:
        node[item] = {}    # create new sub-dictionary
        node = node[item]  # move to new sub-dictionary
    #return node


x = {}

list1 = ['thing1','thing2','thingn']
list2 = ['element1','element2','elementn']

add(x, list1)
add(x, list2)

print(x)
print(x['thing1']['thing2']['thingn'])

x['thing1']['thing2']['newdata'] = "Hello World!"
print(x['thing1']['thing2']['newdata'])
使用
.split('.')
可以使用字符串
'element1.element2.elementn'

add(x, 'element1.element2.elementn'.split('.'))

编辑:类似于获取值

def add(node, levels):
    '''node: starting node'''
    for item in levels:
        node[item] = {}    # create new sub-dictionary
        node = node[item]  # move to new sub-dictionary
    #return node

def get(node, levels):
    '''node: starting node'''
    for item in levels:
        node = node[item]  # move to new sub-dictionary
    return node

x = {}

add(x, ['thing1','thing2','thingn'])
add(x, 'element1.element2.elementn'.split('.'))

x['thing1']['thing2']['newdata'] = "Hello World!"
print(x['thing1']['thing2']['newdata'])

print( get(x, ['thing1', 'thing2', 'newdata']) )
print( get(x, 'thing1.thing2.newdata'.split('.') ) )

我认为行“start=start[item]”将重新初始化变量start,而不仅仅是更新字典中的位置。它让我更深入地了解这个结构,比如在树或图形中从一个节点移动到另一个节点。我很难找到这个变量的好名字,但现在我发布了它应该有name
node
。太棒了!我在Ruby中复制了同样的东西,试图弄清楚它是如何工作的,但我发现它很棘手:o。你能解释一下
x
是如何受到影响的吗(在第一个例子中)?@iGian for list/dictionary Python在函数中使用引用-它不会创建重复的列表/字典-因此函数始终在原始
x
上工作。与使用
y=x时相同-两个变量保持相同的字典。