Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Json 将列表转换为Dict--更新了我的问题_Json_Python 2.7 - Fatal编程技术网

Json 将列表转换为Dict--更新了我的问题

Json 将列表转换为Dict--更新了我的问题,json,python-2.7,Json,Python 2.7,使用以下数据将列表转换为目录: store=[['hostname ', ' ABCD'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T']] 请帮助/建议我如何将上述列表转换为Dict 我尝试了下面的代码,但它没有给我预期的输出 for i in store: print(d

使用以下数据将
列表
转换为
目录

store=[['hostname ', ' ABCD'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T']]
请帮助/建议我如何将上述列表转换为Dict

我尝试了下面的代码,但它没有给我预期的输出

for i in store:
    print(dict(zip(i,i)))
要检查JSON对象的新数组,请执行以下操作:

print("now you have this list of JSON objects : " + str(res)) 
我想指出的是,如果您已经解决了您的问题,任何反馈都将非常感谢,即使只是为了澄清

在多个列表中的拆分改编自:

编辑:

我现在已经看了stackoverflow中的其他问题,这些问题让我觉得您并没有要求将内部列表修复为dict,而只是简单的dict到json的转换。 我不明白那种格式的json对你有什么帮助,但如果是这样的话,你应该在你的问题中指定它。 查看dict格式是非常容易误导的

Edit2

只是:


我注意到您有重复的键,但无法将重复的键添加到字典中,因此您必须迭代这两个子列表并创建两个字典。然后我将这两个字典添加到字典列表中。我希望我理解这个问题。检查我的答案

store=[ ['hostname ','ABCD','fsystem ','/dev/sdb','actual_size ','2.5T'],
        ['hostname ','XYZ','fsystem ','/dev/sdb','actual_size ','2.5T']
      ] 

def convert_to_dictionary(a): 
    it = iter(a) 
    res_dct = dict(zip(it, it)) 
    return res_dct 

dictionaries = []          
#iterate each sublist in store list
for lst in store:
   dictionary = convert_to_dictionary(lst)
   dictionaries.append(dictionary);

for di in dictionaries:
  print(di) 

这回答了你的问题吗@卡洛奇:我已经更新了我的问题。请检查一下您在
dict
{:}中的键和值应该是什么?例如,你想要这个<代码>{'hostname':'ABCD'}?类似这样的代码
print({i[0]:i[1]代表i-in-store})
key:hostname-value:ABCD-key:fsystem-value:/dev/sdbI了解,我是python新手,致力于自动化,因此根据我的需求寻求帮助。。我已经更新了我的问题。如果你能在这里帮助我,请让我知道,那将非常有帮助。让我知道这是否是你想要的。现在,如果您仍然对dict感兴趣,您将获得一个dict数组,这也是标准的json输入格式。如果您想要(在本例中)2个字典,您只需通过for循环从数组中的2个变量中提取它们。这是微不足道的,但也许你说你想要一个类型的DICT输出,而不是一个完美的数组,你能解释我下面两行,这将真正帮助我深入了解iDXYList= = [IDX + 1的IDX,VAR枚举(Store)如果ValtualLoad Save'在Val]中注释空白空间!!!!!!storeNew=[dict(store[i:j])表示zip中的i,j([0]+idx_列表,idx_列表+([size]如果idx_列表[-1]!=size else[])]
storeNew是一个包含两个词典的列表。
store=[['hostname ', ' ABCD'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T']]
size = len(store)
idx_list = [idx + 1 for idx, val in enumerate(store) if 'actual_size ' in val] #note the blank space!!!!
storeNew = [dict(store[i: j]) for i, j in zip([0] + idx_list, idx_list + ([size] if idx_list[-1] != size else []))]  
storeNew #[{'actual_size ': ' 2.5T', 'fsystem ': ' /dev/sdb', 'hostname ': ' ABCD'},{'actual_size ': ' 2.5T', 'fsystem ': ' /dev/sdb', 'hostname ': ' XYZ'}]
store=[ ['hostname ','ABCD','fsystem ','/dev/sdb','actual_size ','2.5T'],
        ['hostname ','XYZ','fsystem ','/dev/sdb','actual_size ','2.5T']
      ] 

def convert_to_dictionary(a): 
    it = iter(a) 
    res_dct = dict(zip(it, it)) 
    return res_dct 

dictionaries = []          
#iterate each sublist in store list
for lst in store:
   dictionary = convert_to_dictionary(lst)
   dictionaries.append(dictionary);

for di in dictionaries:
  print(di)