【发布时间】:2019-12-17 16:49:55
【问题描述】:
给定字典,我如何从字典中获取“原始”值列表?所需的结果在末尾不包含括号或括号或双引号,它只包含每个值的单引号。期望的结果如下所示:
result = 'yellow', 'green', 'white'
我愿意接受有关如何设置字典以轻松获得所需结果输出的建议。字典可以由键值对组成,其中键值对是字典和列表或元组或集合。
此解决方案在测试中有效,我想知道是否有比使用 print 更 Python 和更强大的东西,以便我可以使用结果,而不仅仅是在测试中打印出来:
Dict_w_tuple_values = dict(
dictionary={
'fruits':{
'type':['c'],
'colors':('yellow', 'green', 'white'),
},
)
result = print(", ".join( repr(e) for e in list(Dict_w_tuple_values['dictionary']['fruits']['colors'])))
原来我有这本词典:
Dict_w_list_values = dict(
dictionary={
'fruits':{
'type':['c'],
'colors':['yellow', 'green', 'white'],
},
)
...并尝试了列表推导,.join,eval(list_object),.strip,.lstrip,findall,str(list_object)[1:-1],re.sub,replace,@9876543334@ 和 @987654@3 .
这些都没有奏效。我看到了一些使用正则表达式的机会,但没有得到想要的结果。
最终,我想使用结果来填充参数
.format(**Dict_w_tuple_values)。
填充时的参数应该是:
'yellow', 'green', 'white'
不是
"'yellow', 'green', 'white'"
或
["'yellow', 'green', 'white'"]
也没有
("'yellow', 'green', 'white'")
【问题讨论】:
-
您没有列表对象。你有一个字符串
标签: python dictionary object quotes strip