将包含一项的列表转换为字典值中的项本身
Convert list with one item to item itself in dict value
例如,有一个包含键值对的字典,其中的值是具有不同“内容”的列表。有些列表只有一个元素。这些元素可以是不同类型的数据。
问题:将具有一个元素的列表类型键值转换为dict值中的元素本身的最有效方法是什么?
输入数据
{
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
预期输出数据
{
"key1": "text",
"key2": ["text", "text"],
"key3": {"key0": 0},
"key4": {"key0": 0, "key1": 1},
"key5": "",
"key6": "",
"key7": "text",
"key8": {"key0": 0}
}
我试过的
{k: v[0] for k, v in dct.items()}
带有问题和评论的我的代码输出
{
'key1': 'text',
'key2': 'text', <- ISSUE 1 should be ["text", "text"]
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': <- ISSUE 2 Raises ERROR "IndexError: list index out of range" which is quite expected, should be ""
'key6': [], <- ISSUE 3 should be ""
'key7': ['text'], <- ISSUE 4 should be "text"
'key8': [{'key0': 0}] <- ISSUE 5 should be {'key0': 0}
}
将 Input data
中的字典转换为 Expected output data
中的字典的最有效方法是什么?
为了解决这个问题,我专注于如何转换值:我创建了一个名为 delist
的函数来删除包含 1 个元素的列表:
def delist(value):
while isinstance(value, list) and len(value) == 1:
value = value[0]
if value == []:
value = ""
return value
希望逻辑很容易理解。使用它:
data = {
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
new_data = {k: delist(v) for k, v in data.items()}
而new_data
是
{'key1': 'text',
'key2': ['text', 'text'],
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': '',
'key6': '',
'key7': 'text',
'key8': {'key0': 0}}
也许不是最优雅的(实际上很丑陋)但可以解决问题,以防我不会收到更智能的东西。
# input data
dct = {
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
# define function
def delist(dct):
new_dct = {}
for k, v in dct.items():
if v:
if len(v) > 1:
new_dct[k] = v
else:
if v[0]:
if type(v[0])==list:
new_dct[k] = v[0][0]
else:
new_dct[k] = v[0]
else:
new_dct[k] = ""
else:
new_dct[k] = ""
return new_dct
# apply function to input data
delist(dct)
Returns
{
'key1': 'text',
'key2': ['text', 'text'],
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': '',
'key6': '',
'key7': 'text',
'key8': {'key0': 0}
}
这里有一个 one-liner 使用 np.ravel()
d = {
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
out = {k: list(np.ravel(v)) if np.ravel(v).size>1 else (np.ravel(v)[0] if np.ravel(v).size==1 else '') for k,v in d.items()}
输出:
{'key1': 'text',
'key2': ['text', 'text'],
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': '',
'key6': '',
'key7': 'text',
'key8': {'key0': 0}}
例如,有一个包含键值对的字典,其中的值是具有不同“内容”的列表。有些列表只有一个元素。这些元素可以是不同类型的数据。
问题:将具有一个元素的列表类型键值转换为dict值中的元素本身的最有效方法是什么?
输入数据
{
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
预期输出数据
{
"key1": "text",
"key2": ["text", "text"],
"key3": {"key0": 0},
"key4": {"key0": 0, "key1": 1},
"key5": "",
"key6": "",
"key7": "text",
"key8": {"key0": 0}
}
我试过的
{k: v[0] for k, v in dct.items()}
带有问题和评论的我的代码输出
{
'key1': 'text',
'key2': 'text', <- ISSUE 1 should be ["text", "text"]
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': <- ISSUE 2 Raises ERROR "IndexError: list index out of range" which is quite expected, should be ""
'key6': [], <- ISSUE 3 should be ""
'key7': ['text'], <- ISSUE 4 should be "text"
'key8': [{'key0': 0}] <- ISSUE 5 should be {'key0': 0}
}
将 Input data
中的字典转换为 Expected output data
中的字典的最有效方法是什么?
为了解决这个问题,我专注于如何转换值:我创建了一个名为 delist
的函数来删除包含 1 个元素的列表:
def delist(value):
while isinstance(value, list) and len(value) == 1:
value = value[0]
if value == []:
value = ""
return value
希望逻辑很容易理解。使用它:
data = {
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
new_data = {k: delist(v) for k, v in data.items()}
而new_data
是
{'key1': 'text',
'key2': ['text', 'text'],
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': '',
'key6': '',
'key7': 'text',
'key8': {'key0': 0}}
也许不是最优雅的(实际上很丑陋)但可以解决问题,以防我不会收到更智能的东西。
# input data
dct = {
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
# define function
def delist(dct):
new_dct = {}
for k, v in dct.items():
if v:
if len(v) > 1:
new_dct[k] = v
else:
if v[0]:
if type(v[0])==list:
new_dct[k] = v[0][0]
else:
new_dct[k] = v[0]
else:
new_dct[k] = ""
else:
new_dct[k] = ""
return new_dct
# apply function to input data
delist(dct)
Returns
{
'key1': 'text',
'key2': ['text', 'text'],
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': '',
'key6': '',
'key7': 'text',
'key8': {'key0': 0}
}
这里有一个 one-liner 使用 np.ravel()
d = {
"key1": ["text"],
"key2": ["text", "text"],
"key3": [{"key0": 0}],
"key4": [{"key0": 0, "key1": 1}],
"key5": [],
"key6": [[]],
"key7": [["text"]],
"key8": [[{"key0": 0}]]
}
out = {k: list(np.ravel(v)) if np.ravel(v).size>1 else (np.ravel(v)[0] if np.ravel(v).size==1 else '') for k,v in d.items()}
输出:
{'key1': 'text',
'key2': ['text', 'text'],
'key3': {'key0': 0},
'key4': {'key0': 0, 'key1': 1},
'key5': '',
'key6': '',
'key7': 'text',
'key8': {'key0': 0}}