连接字符串:“乘法"的两个字符串列表 [英] Concatenating Strings: "Multiplication" of two list of strings

查看:104
本文介绍了连接字符串:“乘法"的两个字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关字符串列表, 在此处将乘法运算定义为串联:

For list of strings, define the multiplication operation in as concatenating here:

l1 = ['aa', 'bb', 'cc']
l2 = ['11', '22']
l3 = l1 op l2

预期输出:

l3 = ['aa11', 'aa22', 'bb11', 'bb22', 'cc11', 'cc22']

我们可以使用

for l in l1:
    for ll in l2:
        l3.append(l+ll)

但是,我很高兴听到pythonic解决方案.

But I'd be grateful to hear a pythonic solution.

推荐答案

from itertools import product

l1 = ['aa', 'bb', 'cc']
l2 = ['11', '22']

l3 = [x+y for (x,y) in product(l1,l2)]

print(l3)

但这实际上与您正在做的事情相同(只要您纠正错别字:P)

But it's effectively the same thing as what you're doing (provided you fix the typo :P)

这篇关于连接字符串:“乘法"的两个字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆