问题
For list of strings, define the multiplication operation in as concatenating here:
l1 = ['aa', 'bb', 'cc']
l2 = ['11', '22']
l3 = l1 op l2
Expected output:
l3 = ['aa11', 'aa22', 'bb11', 'bb22', 'cc11', 'cc22']
Simply we can use
for l in l1:
for ll in l2:
l3.append(l+ll)
But I'd be grateful to hear a pythonic solution.
回答1:
from itertools import product
l1 = ['aa', 'bb', 'cc']
l2 = ['11', '22']
l3 = [x+y for (x,y) in product(l1,l2)]
print(l3)
But it's effectively the same thing as what you're doing (provided you fix the typo :P)
回答2:
l3 = [a+b for a in l1 for b in l2]
来源:https://stackoverflow.com/questions/50689672/concatenating-strings-multiplication-of-two-list-of-strings