You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于输入列表生成三种指定规则的字符串拼接组合列表?

问题:如何生成三个符合要求的拼接列表?

给定以下Python初始代码:

#Load the input lists
load_names_list = ['Katherine', 'María Jose', 'Steve']
load_surnames_list = ['Taylor', 'Johnson', 'White', 'Clark']

#Shuffle the data lists: ['element_AA element_AA' , 'element_AA element_AB' , ... ]
names_with_names_list = []
surnames_with_surnames_list = []
names_with_surnames_list = []

print(names_with_names_list)
print(surnames_with_surnames_list)
print(names_with_surnames_list)

需求说明:

  • names_with_names_list:将load_names_list中的每个名字与列表内所有名字拼接(含自身),元素间用空格分隔,示例格式:name_A name_A、name_A name_B;
  • surnames_with_surnames_list:规则与names_with_names_list一致,但基于load_surnames_list生成;
  • names_with_surnames_list:需生成load_names_list和load_surnames_list元素间所有可能的顺序组合拼接(名字在前姓氏在后、姓氏在前名字在后),元素间用空格分隔。

预期输出如下:

#for names_with_names_list
['Katherine Katherine', 'Katherine María Jose', 'Katherine Steve', 'María Jose Katherine', 'María Jose María Jose', 'María Jose Steve', 'Steve Katherine', 'Steve María Jose', 'Steve Steve']

#for surnames_with_surnames_list
['Taylor Taylor', 'Taylor Johnson', 'Taylor White', 'Taylor Clark', 'Johnson Taylor', 'Johnson Johnson', 'Johnson White', 'Johnson Clark', 'White Taylor', 'White Johnson', 'White White', 'White Clark', 'Clark Taylor', 'Clark Johnson', 'Clark White', 'Clark Clark']

#for names_with_surnames_list
['Katherine Taylor', 'Katherine Johnson', 'Katherine White', 'Katherine Clark', 'María Jose Taylor', 'María Jose Johnson', 'María Jose White', 'María Jose Clark', 'Steve Taylor', 'Steve Johnson', 'Steve White', 'Steve Clark', 'Taylor Katherine', 'Taylor María Jose', 'Taylor Steve', 'Johnson Katherine', 'Johnson María Jose', 'Johnson Steve', 'White Katherine', 'White María Jose', 'White Steve', 'Clark Katherine', 'Clark María Jose', 'Clark Steve']

解决方案

方法一:使用itertools.product(推荐)

Python标准库的itertools.product可以直接生成笛卡尔积,完美匹配需求中的“全组合”场景,代码简洁高效:

import itertools

# 输入列表
load_names_list = ['Katherine', 'María Jose', 'Steve']
load_surnames_list = ['Taylor', 'Johnson', 'White', 'Clark']

# 生成同列表内的全组合(含自身)
names_with_names_list = [f"{a} {b}" for a, b in itertools.product(load_names_list, repeat=2)]
surnames_with_surnames_list = [f"{a} {b}" for a, b in itertools.product(load_surnames_list, repeat=2)]

# 生成跨列表的双向组合
name_first = [f"{name} {surname}" for name, surname in itertools.product(load_names_list, load_surnames_list)]
surname_first = [f"{surname} {name}" for surname, name in itertools.product(load_surnames_list, load_names_list)]
names_with_surnames_list = name_first + surname_first

# 验证输出
print(names_with_names_list)
print(surnames_with_surnames_list)
print(names_with_surnames_list)

代码说明:

  • itertools.product(iterable, repeat=2):生成列表与自身的笛卡尔积,即每个元素和列表内所有元素(含自身)的组合;
  • 跨列表组合分两部分生成:先做名字在前的组合,再做姓氏在前的组合,最后合并结果;
  • f-string(f"{a} {b}")用于快速完成字符串拼接,语法简洁。

方法二:嵌套循环实现(无依赖)

如果不想引入外部库,用嵌套循环也能实现相同逻辑,逻辑更直观:

# 输入列表
load_names_list = ['Katherine', 'María Jose', 'Steve']
load_surnames_list = ['Taylor', 'Johnson', 'White', 'Clark']

# 生成names_with_names_list
names_with_names_list = []
for name1 in load_names_list:
    for name2 in load_names_list:
        names_with_names_list.append(f"{name1} {name2}")

# 生成surnames_with_surnames_list
surnames_with_surnames_list = []
for s1 in load_surnames_list:
    for s2 in load_surnames_list:
        surnames_with_surnames_list.append(f"{s1} {s2}")

# 生成names_with_surnames_list
names_with_surnames_list = []
# 名字在前,姓氏在后
for name in load_names_list:
    for surname in load_surnames_list:
        names_with_surnames_list.append(f"{name} {surname}")
# 姓氏在前,名字在后
for surname in load_surnames_list:
    for name in load_names_list:
        names_with_surnames_list.append(f"{surname} {name}")

# 验证输出
print(names_with_names_list)
print(surnames_with_surnames_list)
print(names_with_surnames_list)

这种方式和itertools.product的底层逻辑一致,只是代码稍显冗长,适合需要避免依赖的场景。


内容的提问来源于stack exchange,提问作者Matt095

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:50:20