【问题标题】:Python permutations with repetitions带有重复的 Python 排列
【发布时间】:2021-02-03 14:41:52
【问题描述】:

如果我使用“itertools.permutations”,我不会得到所有可能的组合。 (即“abb”或“cbc”)

import itertools
charList = ["a", "b", "c"]

per = itertools.permutations(charList, 3)

for val in per:
    print(val)

我正在搜索“itertools.product”之类的内容,但它仅适用于数字。

【问题讨论】:

  • 我正在寻找类似“itertools.product”的东西,但它只适用于数字。 - 这根本不是真的......
  • @mousetail 同样的问题
  • list(it.product(charList, repeat=3)) 做这份工作
  • @Tomerikoo 你能帮帮我吗,因为我的情况它不起作用。
  • 这能回答你的问题吗? Generating permutations with repetitions

标签: python combinations permutation itertools


【解决方案1】:
import itertools
x = ['a', 'b', 'c']
print([''.join(p) for p in itertools.product(x, repeat=3)])

将产生:

['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc', 'baa', 'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa', 'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多