将平面 Python 字典转换为字典列表

发布于 2024-12-09 18:23:12 字数 380 浏览 1 评论 0原文

我有一本以下格式的字典,我不知道我将收到的行数或项目数:

{'line(0).item1':'a', 'line(0).item2':'34', 
 'line(1).item1':'sd', 'line(1).item2':'2', 'line(1).item3':'fg', 
 'line(2).item1':'f' ... }

将其解析为以下格式的字典列表的最Pythonic方法是什么:

[{'item1':'a', 'item2':'34'}, 
 {'item1':'sd', 'item2':'2', 'item3':'fg'}, 
 {'item1':'f',...}, ...]

提前致谢。

I have a dictionary in the following format where I don't know the number of lines or items I'm going to receive:

{'line(0).item1':'a', 'line(0).item2':'34', 
 'line(1).item1':'sd', 'line(1).item2':'2', 'line(1).item3':'fg', 
 'line(2).item1':'f' ... }

What is the most pythonic way to parse this into a list of dictionaries in the following format:

[{'item1':'a', 'item2':'34'}, 
 {'item1':'sd', 'item2':'2', 'item3':'fg'}, 
 {'item1':'f',...}, ...]

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-12-16 18:23:12
d = {'line(0).item1':'a' ...}

out = collections.defaultdict(list)
for k,v in d.items():
    n,val = re.findall(r'^line\((\d+)\)\.(\w+)

输出将是预期的:

[{'item2': '34', 'item1': 'a'}, {'item2': '2', 'item3': 'fg', 'item1': 'sd'}, {'item1': 'f'}]
, k)[0] out[int(n)].append((val,v)) my_list = [dict(out[v]) for v in sorted(out)]

输出将是预期的:

d = {'line(0).item1':'a' ...}

out = collections.defaultdict(list)
for k,v in d.items():
    n,val = re.findall(r'^line\((\d+)\)\.(\w+)

and the output will be the expected:

[{'item2': '34', 'item1': 'a'}, {'item2': '2', 'item3': 'fg', 'item1': 'sd'}, {'item1': 'f'}]
, k)[0] out[int(n)].append((val,v)) my_list = [dict(out[v]) for v in sorted(out)]

and the output will be the expected:

樱&纷飞 2024-12-16 18:23:12

我会使用字典的中间字典,因为无法知道末尾有多少“行”,并且以后无法在列表末尾插入新元素。

它应该足够简单,可以迭代此 dict 中的每个元素并将其解析为行号和键。然后,您可以轻松地将新的dict传输到您想要的列表中。一个可能的实现可能是:

intermediate_dict = {}
for entry in my_dict:
    line_string, key = entry
    line_number = int(line_string[line_string.index('(') + 1: line_string.index(')')])
    if line_number not in intermediate_dict:
        intermediate_dict[line_number] = {}
    intermediate_dict[line_number][key] = my_dict[entry]

new_list = []
for i in xrange(len(intermediate_dict)):
    new_list.append(intermediate_dict[i])

I'd go with an intermediate dictionary of dictionaries, since there is no way of knowing how many "lines" you'd have at the end and you can't insert a new element at the end of the list later.

It should be simple enough to iterate over every element in this dict and parse it into the line number and key. You could then easily transfer the new dict into the list you desired. A possible implementation could be:

intermediate_dict = {}
for entry in my_dict:
    line_string, key = entry
    line_number = int(line_string[line_string.index('(') + 1: line_string.index(')')])
    if line_number not in intermediate_dict:
        intermediate_dict[line_number] = {}
    intermediate_dict[line_number][key] = my_dict[entry]

new_list = []
for i in xrange(len(intermediate_dict)):
    new_list.append(intermediate_dict[i])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文