Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个这样的for循环:
for i in conversion: for f in glob.glob(i): print(os.path.getsize(f))
我想将其转换为列表理解:
试过这个:
[os.path.getsize(f) for f in glob.glob(i) for i in conversion]
但没有用。
for双列表推导式中的循环顺序与嵌套循环使用的顺序相同:
for
[os.path.getsize(f) for i in conversion for f in glob.glob(i)]
这有点令人困惑,因为您希望内部循环更“内部”,但是一旦您意识到它与嵌套循环的顺序相同,一切都很容易:)