在Python中查找组合而不导入itertools

发布于 2024-09-26 13:02:13 字数 494 浏览 6 评论 0原文

我希望在 Python 中完成以下任务而不导入任何模块。

我的代码包括

Two List
---------
list1=['aun','2ab','acd','3aa']
list2=['ca3','ba2','dca','aa3']

Function
---------

  • 从 list1 生成 2 个项目组合
  • 从 list2 生成 2 个项目组合
  • 从 list1 和 list2 生成 2 个项目组合

我不需要打印这两个项目的所有组合

,但我想传递所有这 2 个项目组合进一步完成任务并显示结果

 analysize R.. **ca3** .... and ... **2ab** // Combinations of two items from list1 and list2

 Print analysize

I want following task to be done in Python without importing any modules.

My Code consists

Two List
---------
list1=['aun','2ab','acd','3aa']
list2=['ca3','ba2','dca','aa3']

Function
---------

Where it will:

  • Generates 2 items combination from list1
  • Generates 2 items combination from list2
  • Generates 2 items combination from list1 and list2

I don't need to print these all combinations of two items

But I want to pass all these 2 items combinations to further task and show results

 analysize R.. **ca3** .... and ... **2ab** // Combinations of two items from list1 and list2

 Print analysize

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

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

发布评论

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

评论(1

北斗星光 2024-10-03 13:02:13

好吧,您已经得到了如何使用 itertools 进行操作的答案。如果您想在不导入该模块的情况下执行此操作(无论出于何种原因......),您仍然可以查看 文档 并阅读源代码

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)

Well, you already got the answer how to do it with itertools. If you want to do it without importing that module (for whatever reason...), you could still take a look at the docs and read the source:

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

and

def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文