2024-03-28 19:00:50 发布
网友
给定集合字典:
{1: {2}, 2: {1, 3, 5}, 3: {2, 4}, 4: {3, 7}, 5: {2, 6}, 6: {5}, 7: {8, 4}, 8: {7}}
有没有什么东西我可以很容易地把它变成一本清单字典?你知道吗
期望输出:
{1: [2], 2: [1, 3, 5], 3: [2, 4], 4: [3, 7], 5: [2, 6], 6: [5], 7: [8, 4], 8: [7]}
使用list()函数使用dict comprehension和convert set to list
a = {1: {2}, 2: {1, 3, 5}, 3: {2, 4}, 4: {3, 7}, 5: {2, 6}, 6: {5}, 7: {8, 4}, 8: {7}} {k:list(v) for k,v in a.items()} Out[274]: {1: [2], 2: [1, 3, 5], 3: [2, 4], 4: [3, 7], 5: [2, 6], 6: [5], 7: [8, 4], 8: [7]}
result = {key: list(values) for key, values in dictionary.items()}
使用list()函数使用dict comprehension和convert set to list
相关问题 更多 >
编程相关推荐