[英]how to store dictionary keys and values in a given format
我有一本字典,我需要將鍵和值存儲在列表中
例如:
dic = {'name':['Jon', 'max'],'add':['sector1','sector2']}
我需要提取每個鍵及其值並存儲在列表中
output = [['name;jon;max'],[add;sector1;sector2]]
方法如下:
>>> d = {'name':['Jon', 'max'],'add':['sector1','sector2']}
>>> [[";".join((k, *v))] for k, v in d.items()]
Output:
[['name;Jon;max'], ['add;sector1;sector2']]
如果您正在尋找更簡單的解決方案,那么您可以使用它
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.