简体   繁体   中英

Python nested for loops using list comprehension

for module in host.modules:
    for removal in removal_re:
        compile_re = re.compile(removal)
        if compile_re.match(module.name):
            removed_modules.append(module)
            continue

How am I able to do the above using list comprehension? I've tried researching a lot on list comp but can't seem to get my head around it for some reason. Any ideas/suggestions?

You can use any to add the module to the list if any of the regex matches without evaluating them all. Also, re.compile does not really help if you use the regex just once:

removed_modules = [module for module in host.modules
                   if any(re.match(removal, module.name) for removal in removal_re)]

(Note: I assume you meant break instead of continue , which does nothing in your code and would result in module being added multiple times to the list if it matches more than one regex.)

Also, despite what other answers say (some of which seem to misunderstand what the loop is doing), I think in this case, the list comprehension is more readable and clearer than the loop.

Generally, you can somehow translate loops into list comprehensions with the following scheme:

new_list = []

for element in old_list:
    if condition:
        new_element = func(element)
        new_list.append(new_element)

Does the same trick as:

new_list = [func(element) for element in old_list if condition]

The func can be anything that alters the original list element. Maybe this helps you to get your head around list comprehensions.

your code is much more readable than a list comprehension, but let's walk through it

the typical syntax is something like this

 [expression(item) for item in list if condition]

this will return a list, if you don't assign the list comp to a variable then it will just perform the expression len(list) times once for ever item in the list. the if statement is optional. knowing this let's change your code up a little bit, you can see the hard part is getting the inner loop done, so let's do that first

for module in host.modules:
    removed_modules = [module for removal in removal_re if re.compile(removal).match(module.name)]

ok so that's already not pep 8 compliant because it's too long but for the sake of argument you can see we made a list of removed modules and we're looping through the modules in host.modules doing this over and over.

this means that what's under this new for loop can become the expression we talked about earlier, and the module can become the item and we end up with this

[[module for removal in removal_re if re.compile(removal).match(module.name)] for module in host.modules]

hey presto a one liner but just remember, just because you can do something doesn't mean that you should!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM