更新时间:2022-11-22 11:27:36
lst = [j + k for j in s1 for k in s2]
或
lst = [(j, k) for j in s1 for k in s2]
如果你想要元组.
就像问题中一样,for j...
是外循环,for k...
是内循环.
Like in the question, for j...
is the outer loop, for k...
is the inner loop.
本质上,您可以在列表推导式中根据需要拥有任意数量的独立for x in y"子句,只需一个接一个地粘贴即可.
Essentially, you can have as many independent 'for x in y' clauses as you want in a list comprehension just by sticking one after the other.
为了使其更具可读性,请使用多行:
To make it more readable, use multiple lines:
lst = [
j + k # result
for j in s1 # for loop
for k in s2 # for loop
# condition
]