如何将带键值对的列表转换为字典。

10

我想遍历这个列表

['name: test1', 'email: test1@gmail.com', 'role: test', 'description: test', 'name: test2', 'email: test2@gmail.com', 'role: test2', 'description: test2', 'name: test3', 'email: test3@gmail.com', 'role: test3', 'description: test3']

并为每个组返回一个字典列表。例如

[{name: 'test', email:'test@gmail.com', role:'test', description:'test'}, {name: 'test2', email:'test2@gmail.com', role:'test2', description:'test2'}]

我已经尝试通过,(逗号)拆分列表并搜索其中的'name:'来返回name等字段,但是我无法将其与电子邮件、角色等联系起来。

提前感谢您的任何帮助。


这个条目列表是否保证按照那个顺序排列,还是元素可以以任何顺序出现? - rayryeng
输入中每个组的大小是否固定?如果不是,那么如何确定组边界? - GZ0
5个回答

7

无需提前知道每个字典中键的数量,您可以遍历列表,通过': '将每个字符串拆分为键和值,如果键已经在最后一个字典中,则向列表附加新字典,并通过该键将值添加到最后一个字典中:

output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value

假设您的样本列表存储在lst中,output将变为:

[{'name': 'test1',
  'email': 'test1@gmail.com',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': 'test2@gmail.com',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': 'test3@gmail.com',
  'role': 'test3',
  'description': 'test3'}]

3

我假设你的订单总是一样的,即每组有4个。我们的想法是使用:拆分字符串,然后创建键/值对并使用嵌套的for循环。使用.strip()可以去除空格。

lst = ['name: test1', 'email: test1@gmail.com', 'role: test', 'description: test', 
       'name: test2', 'email: test2@gmail.com', 'role: test2', 'description: test2', 
       'name: test3', 'email: test3@gmail.com', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': 'test1@gmail.com',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': 'test2@gmail.com',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': 'test3@gmail.com',  'role': 'test3',  'description': 'test3'}]

一种列表推导式的写法如下:
answer = [{j.split(':')[0]:j.split(':')[1].strip() for j in lst[i:i+4]} for i in range(0, len(lst), 4)]

3
你可以做以下的事情:
dictionary = dict()
all_dictionaries = []
for index , value  in  [x.split(": ") for x in A] :
     if index in dictionary :
         all_dictionaries .append(dictionary )
         dictionary = dict()
     else :
       dictionary [index] = value
all_dictonaries.append(dictionary)

2
如果列表中的数据形式保证始终与问题示例中的方式相同,则可以这样做:最初的回答
L = ['name: test1', 'email: test1@gmail.com', 'role: test', 'description: test', 'name: test2', 'email: test2@gmail.com', 'role: test2', 'description: test2', 'name: test3', 'email: test3@gmail.com', 'role: test3', 'description: test3']

A = []

for i in range(0, len(L), 4):
  D = {}
  for p in L[i:i + 4]:
    k, v = map(str.strip, p.split(':'))
    D[k] = v
  A.append(D)

from pprint import pprint
pprint(A)

输出:

[{'description': 'test',
  'email': 'test1@gmail.com',
  'name': 'test1',
  'role': 'test'},
 {'description': 'test2',
  'email': 'test2@gmail.com',
  'name': 'test2',
  'role': 'test2'},
 {'description': 'test3',
  'email': 'test3@gmail.com',
  'name': 'test3',
  'role': 'test3'}]

1
这个解决方案假设每个组的大小恰好为4。
l = ['name: test1', 'email: test1@gmail.com', 'role: test', 'description: test', 
     'name: test2', 'email: test2@gmail.com', 'role: test2', 'description: test2',
     'name: test3', 'email: test3@gmail.com', 'role: test3', 'description: test3']

output = [dict(s.split(": ") for s in l[i:i+4]) for i in range(0, len(l), 4)]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接