I have the following list:
result = [{"{'rueda-pinchada', 'rueda-repuesto'}", " {'eje', 'maletero', 'suelo'}"}]
and I want to get the following:
result = [{'rueda-repuesto', 'rueda-pinchada'}, {'maletero', 'eje', 'suelo'}]
As you can see, the second list contains two sets, while the first contains a set with two strings that "mimic" a set. The idea is to flatten the list to get the two strings, and with the list of strings, convert these to set. Remaining a list of sets.
I have tried to flatten it as follows:
aux = [constante for x in result for constante in x]
and has resulted in:
['{', "'", 'r', 'u', 'e', 'd', 'a', '-', 'p', 'i', 'n', 'c', 'h', 'a', 'd', 'a', "'", ',', ' ', "'", 'r', 'u', 'e', 'd', 'a', '-', 'r', 'e', 'p', 'u', 'e', 's', 't', 'o', "'", '}', ' ', '{', "'", 'e', 'j', 'e', "'", ',', ' ', "'", 'm', 'a', 'l', 'e', 't', 'e', 'r', 'o', "'", ',', ' ', "'", 's', 'u', 'e', 'l', 'o', "'", '}']
We have to untangle the initial expression a bit:
result
it is a list. This list contains a set with two elements of type string :The content of each string is the text representation of a set .
The function
eval
can take a python code, execute it and return the result produced. In this case, the code is simply defining a set .So if we run:
it will be seen that the argument (as string ) creates a set.
eval
will return that set.Code
Using list comprehension, we loop through the set evaluating its elements and appending them to the final list
show
produces: