List comprehension on a nested list?

后端 未结 12 1152
情话喂你
情话喂你 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:40

    Not sure what your desired output is, but if you're using list comprehension, the order follows the order of nested loops, which you have backwards. So I got the what I think you want with:

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

    The principle is: use the same order you'd use in writing it out as nested for loops.

提交回复
热议问题