How to convert this for loop in list comprehension?

后端 未结 1 1894
情深已故
情深已故 2021-01-18 00:14

I have a for loop like this:

for i in conversion:
    for f in glob.glob(i):
        print(os.path.getsize(f))

I want to convert this into

相关标签:
1条回答
  • 2021-01-18 00:58

    The order of the for loops in a double list comprehension is the same order that you would use with nested loops:

    [os.path.getsize(f) for i in conversion for f in glob.glob(i)]
    

    It's a bit confusing because you expect the inner loop to be more "inner", but once you realize it is the same order as the nested loop, everything is easy :)

    0 讨论(0)
提交回复
热议问题