[英]how to store dictionary keys and values in a given format
I have a dictionary and I need to store the key and values in a list我有一本字典,我需要将键和值存储在列表中
for example:例如:
dic = {'name':['Jon', 'max'],'add':['sector1','sector2']}
I need to extract each key and its value and store in a list我需要提取每个键及其值并存储在列表中
output = [['name;jon;max'],[add;sector1;sector2]]
Here's how to do it:方法如下:
>>> d = {'name':['Jon', 'max'],'add':['sector1','sector2']}
>>> [[";".join((k, *v))] for k, v in d.items()]
Output: Output:
[['name;Jon;max'], ['add;sector1;sector2']]
if you are looking for a simpler solution then you can use this如果您正在寻找更简单的解决方案,那么您可以使用它
Data = {'name':['Jon', 'max'],'add':['sector1','sector2']}
output = []
for key, value in Data.items():
formated = key + ";"
formated += ";".join(value)
output.append([formated])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.