List comprehension on a nested list?

后端 未结 12 917
情话喂你
情话喂你 2020-11-22 07:57

I have this nested list:

l = [[\'40\', \'20\', \'10\', \'30\'], [\'20\', \'20\', \'20\', \'20\', \'20\', \'30\', \'20\'], [\'30\', \'20\', \'30\', \'50\', \'         


        
12条回答
  •  孤街浪徒
    2020-11-22 08:22

    Here is how you would do this with a nested list comprehension:

    [[float(y) for y in x] for x in l]
    

    This would give you a list of lists, similar to what you started with except with floats instead of strings. If you want one flat list then you would use [float(y) for x in l for y in x].

提交回复
热议问题