从列表中提取字典并将其附加到一个空的元组中

时间:2018-10-31 20:37:33

标签: python list dictionary tuples

我有一个复杂的列表,其中包含多个词典:

['{"id":"bc50c8bd-7c30-48bf-8467-104d2351aaa2","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}},{"id":"fa42e991-b094-48c2-8fbd-d5cbfd1f9975","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}},{"id":"6d5c4bf5-e790-4695-a8d7-3ebba4a39231","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}}']

我的目标是提取列表中的词典并将输出转换为元组,因此理想的最终结果将是由原始列表中的所有词典组成的元组。在上面的示例中,我希望输出为:

({"id":"bc50c8bd-7c30-48bf-8467-104d2351aaa2","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}},{"id":"fa42e991-b094-48c2-8fbd-d5cbfd1f9975","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}},{"id":"6d5c4bf5-e790-4695-a8d7-3ebba4a39231","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}})

假设“ data_match”是由字典字符串组成的列表的名称,那么挑战在于“ data_match”整体上是单个字符串,因此如果我执行“ data_match [0]”,它将返回整个字符串:

'{"id":"bc50c8bd-7c30-48bf-8467-104d2351aaa2","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}},{"id":"fa42e991-b094-48c2-8fbd-d5cbfd1f9975","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}},{"id":"6d5c4bf5-e790-4695-a8d7-3ebba4a39231","class":"person","type":"box","coordinates":{"x":null,"y":null,"w":null,"h":null}}'

如果我可以从列表中删除开头和结尾的撇号,那将是一个字典列表而不是字符串列表,那将是很好。但是我不知道该怎么做。任何建议将不胜感激!

p.s。这个问题不同于Convert a String representation of a Dictionary to a dictionary?。因为该问题中的字符串仅包含一个字典,而该字符串中没有任何多余的内容,而在我的问题中,列表中的字符串包含多个字典。

0 个答案:

没有答案