Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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中创建dict_Python_List_Dictionary - Fatal编程技术网

向列表项添加字符串,然后在python中创建dict

向列表项添加字符串,然后在python中创建dict,python,list,dictionary,Python,List,Dictionary,我有两个列表,我想用每个列表创建dict,其中键值是一个字符串,然后将这两个dict组合成一个,下面是我的列表: list_1 : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] list_2 : ['BACKUP_INFO', 'sqlite_sequence', 'BACKUP_INFO_SEARCH', 'BACKUP_INFO_SEARCH_content', 'BACKUP_INFO_SEARCH_segments', 'B

我有两个列表,我想用每个列表创建dict,其中键值是一个字符串,然后将这两个dict组合成一个,下面是我的列表:

list_1 :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

list_2 :  ['BACKUP_INFO', 'sqlite_sequence', 'BACKUP_INFO_SEARCH', 'BACKUP_INFO_SEARCH_content', 'BACKUP_INFO_SEARCH_segments', 'BACKUP_INFO_SEARCH_segdir', 'BACKUP_INFO_SEARCH_docsize', 'BACKUP_INFO_SEARCH_stat', 'FILE_INFO', 'FILE_INFO_SEARCH', 'FILE_INFO_SEARCH_content', 'FILE_INFO_SEARCH_segments', 'FILE_INFO_SEARCH_segdir', 'FILE_INFO_SEARCH_docsize', 'FILE_INFO_SEARCH_stat']
列表_1应添加dict键值作为“id”

列表2应添加dict键值作为“表”

然后,应将上述两个dict组合成一个dict,以形成类似的内容:

{
    "output": 
     {
        "id": 1,
        "table" : BACKUP_INFO
     }
     {
        "id": 2,
        "table" :sqlite_sequence 
     }    
}
但是,我使用

table_list_out=dict(zip(list_1,list_2))
返回{'output':{'id':list_1,'table_name':list_2}

{
    "output": {
        "id": [
            1, 
            2, 
            3, 
            4, 
            5, 
            6, 
            7, 
            8, 
            9, 
            10, 
            11, 
            12, 
            13, 
            14, 
            15
        ], 
        "table_name": {
            "1": "BACKUP_INFO", 
            "2": "sqlite_sequence", 
            "3": "BACKUP_INFO_SEARCH", 
            "4": "BACKUP_INFO_SEARCH_content", 
            "5": "BACKUP_INFO_SEARCH_segments", 
            "6": "BACKUP_INFO_SEARCH_segdir", 
            "7": "BACKUP_INFO_SEARCH_docsize", 
            "8": "BACKUP_INFO_SEARCH_stat", 
            "9": "FILE_INFO", 
            "10": "FILE_INFO_SEARCH", 
            "11": "FILE_INFO_SEARCH_content", 
            "12": "FILE_INFO_SEARCH_segments", 
            "13": "FILE_INFO_SEARCH_segdir", 
            "14": "FILE_INFO_SEARCH_docsize", 
            "15": "FILE_INFO_SEARCH_stat"
        }
    }
} 

您可以使用列表:

list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
list_2 = ['BACKUP_INFO', 'sqlite_sequence', 'BACKUP_INFO_SEARCH', 'BACKUP_INFO_SEARCH_content', 'BACKUP_INFO_SEARCH_segments', 'BACKUP_INFO_SEARCH_segdir', 'BACKUP_INFO_SEARCH_docsize', 'BACKUP_INFO_SEARCH_stat', 'FILE_INFO', 'FILE_INFO_SEARCH', 'FILE_INFO_SEARCH_content', 'FILE_INFO_SEARCH_segments', 'FILE_INFO_SEARCH_segdir', 'FILE_INFO_SEARCH_docsize', 'FILE_INFO_SEARCH_stat']
new_dict = {'output':[{'id':a, 'table':b} for a, b in zip(list_1, list_2)]}
输出:

{'output': [{'table': 'BACKUP_INFO', 'id': 1}, {'table': 'sqlite_sequence', 'id': 2}, {'table': 'BACKUP_INFO_SEARCH', 'id': 3}, {'table': 'BACKUP_INFO_SEARCH_content', 'id': 4}, {'table': 'BACKUP_INFO_SEARCH_segments', 'id': 5}, {'table': 'BACKUP_INFO_SEARCH_segdir', 'id': 6}, {'table': 'BACKUP_INFO_SEARCH_docsize', 'id': 7}, {'table': 'BACKUP_INFO_SEARCH_stat', 'id': 8}, {'table': 'FILE_INFO', 'id': 9}, {'table': 'FILE_INFO_SEARCH', 'id': 10}, {'table': 'FILE_INFO_SEARCH_content', 'id': 11}, {'table': 'FILE_INFO_SEARCH_segments', 'id': 12}, {'table': 'FILE_INFO_SEARCH_segdir', 'id': 13}, {'table': 'FILE_INFO_SEARCH_docsize', 'id': 14}, {'table': 'FILE_INFO_SEARCH_stat', 'id': 15}]}

从表面上看,你想要的输出是不可能的。请注意,键“output”对应多个值

可能是这样的,其中与“output”对应的值是一个字典列表

return {'output': [{'id': x, 'table': y} for x, y in zip(list1, list2)]}

你可以循环一下,我肯定有一行,但这很清楚

output = {"output":{} }
for i in xrange(0, len(list_1)):
    output["output"][list_2[i]] = list_1[i]

print output

您想要的输出不是有效的字典。您很容易就搞定了它…感谢it@PetPan很乐意帮忙!