Post #949:
aM
📋 Metadata
- Author: roaoao
- Date: 2017-09-21 16:01:34
- Type:
answer - Quality Score: 8/10
- Reply to: post_00947
🏷️ Tags
python-scripting intermediate advanced solved
⚙️ Related PME Features
- Python Scripting
- Macro Editor
💬 Content
aMars:
Is it possible to run a for loop in the command area or is it necessary to use an external script?
As Way2Close said, you can use List Comprehensions.
For example this code:
for a in range(5):
if a < 2:
b = a * 2
print(b)
same as:
[(locals().update(b=a * 2), print(locals()["b"])) for a in range(5) if a < 2]
Note that we have to mark local variables (c) as global if we want to use it in list comprehensions:
c = 2
for a in range(5):
if a < 2:
b = a * c
print(b)
global c; c = 2; [(locals().update(b=a * c), print(locals()["b"])) for a in range(5) if a < 2]