我将如何在 python 中完成以下操作:

first = ['John', 'David', 'Sarah']
last = ['Smith', 'Jones']

combined = ['John Smith', 'John Jones', 'David Smith', 'David Jones', 'Sarah Smith', 'Sarah Jones']

有没有一种方法可以组合所有排列?

最佳答案

itertools.product

import itertools
combined = [f + ' ' + l for f, l in itertools.product(first, last)]

关于Python 排列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10369300/

10-12 16:42