简体   繁体   中英

Conditionals Nested Loops in List Comprehension

How to compress this whole loop into one single line. Is there any way?

aa = []  
for x in args:
   for y in args:
     if x == y:
       pass
     else:
       kk = x*y
       aa.append(kk)

from itertools import product
aa = [x*y for x,y in product(args, args) if x != y]

Absolutely nothing wrong with previous answer. A double loop also works and might be a little simpler to understand.

[x * y for x in args for y in args if x != y]

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