创建可能案例的简单方法

发布于 2024-08-26 12:23:27 字数 263 浏览 7 评论 0原文

我有数据列表,例如

a = [1,2,3,4]
b = ["a","b","c","d","e"]
c = ["001","002","003"]

我想创建一个新的另一个列表,该列表是由 a、b、c 的所有可能情况混合而成的 是否

d = ["1a001","1a002","1a003",...,"4e003"]

有任何模块或方法可以生成 d 而无需编写许多 for 循环?

I have lists of data such as

a = [1,2,3,4]
b = ["a","b","c","d","e"]
c = ["001","002","003"]

And I want to create new another list that was mixed from all possible case of a,b,c like this

d = ["1a001","1a002","1a003",...,"4e003"]

Is there any module or method to generate d without write many for loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相思故 2024-09-02 12:23:27
[''.join(str(y) for y in x) for x in itertools.product(a, b, c)]
[''.join(str(y) for y in x) for x in itertools.product(a, b, c)]
懷念過去 2024-09-02 12:23:27

如果您也将 a 转换为 str 列表,那么这样做会更简单。
并节省了对 b 和 c 的每个元素不必要的调用 str()

>>> from itertools import product
>>> a = [1,2,3,4]
>>> b = ["a","b","c","d","e"]
>>> c = ["001","002","003"]
>>> a = map(str,a)                   # so a=['1', '2', '3', '4']
>>> map(''.join, product(a, b, c))
['1a001', '1a002', '1a003', '1b001', '1b002', '1b003', '1c001', '1c002', '1c003', '1d001', '1d002', '1d003', '1e001', '1e002', '1e003', '2a001', '2a002', '2a003', '2b001', '2b002', '2b003', '2c001', '2c002', '2c003', '2d001', '2d002', '2d003', '2e001', '2e002', '2e003', '3a001', '3a002', '3a003', '3b001', '3b002', '3b003', '3c001', '3c002', '3c003', '3d001', '3d002', '3d003', '3e001', '3e002', '3e003', '4a001', '4a002', '4a003', '4b001', '4b002', '4b003', '4c001', '4c002', '4c003', '4d001', '4d002', '4d003', '4e001', '4e002', '4e003']

这是时间比较

timeit astr=map(str,a);map(''.join, product(astr, b, c))
10000 loops, best of 3: 43 us per loop

timeit [''.join(str(y) for y in x) for x in product(a, b, c)]
1000 loops, best of 3: 399 us per loop

This is a little simpler to do if you convert a to be a list of str too.
and saves needlessly calling str() on each element of b and c

>>> from itertools import product
>>> a = [1,2,3,4]
>>> b = ["a","b","c","d","e"]
>>> c = ["001","002","003"]
>>> a = map(str,a)                   # so a=['1', '2', '3', '4']
>>> map(''.join, product(a, b, c))
['1a001', '1a002', '1a003', '1b001', '1b002', '1b003', '1c001', '1c002', '1c003', '1d001', '1d002', '1d003', '1e001', '1e002', '1e003', '2a001', '2a002', '2a003', '2b001', '2b002', '2b003', '2c001', '2c002', '2c003', '2d001', '2d002', '2d003', '2e001', '2e002', '2e003', '3a001', '3a002', '3a003', '3b001', '3b002', '3b003', '3c001', '3c002', '3c003', '3d001', '3d002', '3d003', '3e001', '3e002', '3e003', '4a001', '4a002', '4a003', '4b001', '4b002', '4b003', '4c001', '4c002', '4c003', '4d001', '4d002', '4d003', '4e001', '4e002', '4e003']

Here is a timing comparison

timeit astr=map(str,a);map(''.join, product(astr, b, c))
10000 loops, best of 3: 43 us per loop

timeit [''.join(str(y) for y in x) for x in product(a, b, c)]
1000 loops, best of 3: 399 us per loop
疧_╮線 2024-09-02 12:23:27
map(''.join, itertools.product(map(str, a), b, c))
map(''.join, itertools.product(map(str, a), b, c))
梦过后 2024-09-02 12:23:27

这个怎么样?使用地图

print [''.join(map(str,y)) for y in itertools.product(a,b,c)]

How about this? Using map

print [''.join(map(str,y)) for y in itertools.product(a,b,c)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文