我想使用列表理解将
metadata.index.to_list()
中的字符串连接到 : set()
子字符串并将它们存储为 `` 字典。列表的长度是未定义,以下只是一个示例。
我的尝试:
uuids_to_zarr_files = dict([uuid + ": set()" for uuid in metadata.index.to_list()])
追溯:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [29], in <cell line: 1>()
----> 1 uuids_to_zarr_files = dict([uuid + ": set()" for uuid in metadata.index.to_list()])
ValueError: dictionary update sequence element #0 has length 39; 2 is required
输入:
metadata.index.to_list()
['07317dfade92994d6fbbe9faef1236f7',
'91890e05edbd0a767092e10502b3ea99',
'56b00177fa2bbd091a10dae8d57c9539']
预期输出:
uuids_to_zarr_files = {'07317dfade92994d6fbbe9faef1236f7': set(), '91890e05edbd0a767092e10502b3ea99': set(), '56b00177fa2bbd091a10dae8d57c9539': set()}
你列出的理解会做你想要它做的事情。
但是,你为什么认为
dict(["07317dfade92994d6fbbe9faef1236f7: set()"])
除了引发错误之外应该做什么?您是否期望 dict
构造函数能够理解您自己的定制格式?
基本上,你只是想要
{uuid: set() for uuid in metadata.index.to_list()}
或者
dict([(uuid, set()) for uuid in metadata.index.to_list()])
试试这个代码:
arr = ['07317dfade92994d6fbbe9faef1236f7','91890e05edbd0a767092e10502b3ea99','56b00177fa2bbd091a10dae8d57c9539']
dict(zip(arr, [set()]*3))