Home > front end >  Python - transform list of strings/bools into list of lists of strings/bools
Python - transform list of strings/bools into list of lists of strings/bools

Time:12-06

I'm striving to obtain something like:

final = [[False, False, False, False, False],[False, False, False, False, False],[False, True, True, True, True]]

from this:

original = ['False False False False False', 'False False False False False', 'False True True True True']

Does anyone can explain me how to reach the first list?

original = ['False False False False False', 'False False False False False', 'False True True True True']

final = []
for i in range(3):
    final.append([])
    for j in range(len(original)):
        ????

CodePudding user response:

You have list of 3 strings. To convert it to list of booleans you can do:

original = [
    "False False False False False",
    "False False False False False",
    "False True True True True",
]

final = [[b == "True" for b in s.split()] for s in original]
print(final)

Prints:

[
    [False, False, False, False, False],
    [False, False, False, False, False],
    [False, True, True, True, True],
]
  • Related