【问题标题】:Combinations with limited repeats in PythonPython中有限重复的组合
【发布时间】:2016-01-16 22:45:58
【问题描述】:

我知道如何在 Python 中使用 itertools 获取列表的所有组合,但如果我想限制重复次数怎么办?

所以,如果我有 [1, 2, 3, 4, 5]

但我想将组合限制为每个项目仅重复 3 次(最终列表的长度固定,例如 10):

[1, 1, 1, 2, 3, 3, 5, 5, 5, 4]
[1, 2, 3, 3, 3, 4, 5, 5, 4, 4]
[4, 4, 1, 1, 1, 5, 2, 2, 2, 3]

等等。我该怎么做?

【问题讨论】:

  • 重复元素可以是随机的还是需要一些特定的模式?
  • 它们可以是随机的,我只是不想得到类似的结果: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] [1, 1, 1, 1, 1, 1, 1, 1, 1, 2 ] [1, 1, 1, 1, 1, 1, 1, 1, 1, 3 ]

标签: python limit combinations itertools


【解决方案1】:

这可行:

import random

L = [1, 2, 3, 4, 5]
L3 = L * 3
random.shuffle(L3)
L3[:10]

【讨论】:

  • 有什么办法可以确保我得到所有可能的结果?
  • 这是从所有组合中随机选择组合的一种非常优雅的方式……但我认为 OP 想要的是集合,而不是其中的样本。
  • @TomZych 我认为你是对的。最初的问题在这里并不是很清楚,但 cmets 表明了这一点。也许我的建议对其他人有帮助。
【解决方案2】:

我不知道是否有更优雅的方式,但这似乎有效:

from itertools import combinations_with_replacement
a = [1, 2, 3, 4, 5] # can contain letters as well
all_combinations = combinations_with_replacement(a, 10)

filtered_results = []
for current in all_combinations:
   throw_away = False
   for item in current:
      if current.count(item) > 3:
        throw_away = True
        break
   if not throw_away:
     filtered_results.append( current )

for j in filtered_results:
   print(j)

【讨论】:

  • 如果我也想写信怎么办?
  • 无需任何更改也能正常工作。试试 a = ['a', 'b', 1, 2, 3]
【解决方案3】:

你可以这样做:使用 [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5] 并使用 itertools.combination(上面的列表,10)。这意味着每个元素在结果列表中最多出现 3 次。

较小列表的示例:

编辑以确保每个组合只出现一次,组合以随机顺序出现,最后,每个组合都有一个随机顺序。 还结合了 Mike Müller 的想法,将列表乘以 3。

import itertools
import random
combinations = set()
for c in itertools.combinations(sorted([1, 2, 3] * 3), 5):
    combinations.add(c)
shuffledCombinations = list(combinations)
random.shuffle(shuffledCombinations)
for combination in shuffledCombinations:
    copiedCombination = list(combination)
    random.shuffle(copiedCombination)
    print copiedCombination

这将给出:

[2, 1, 3, 2, 2]
[2, 2, 1, 1, 2]
[3, 3, 3, 1, 2]
[2, 2, 3, 2, 3]
[1, 2, 3, 1, 2]
[1, 1, 1, 2, 3]
[2, 1, 1, 1, 2]
[3, 3, 1, 1, 1]
[1, 3, 3, 3, 1]
[3, 2, 3, 2, 3]
[3, 2, 1, 2, 3]
[1, 1, 2, 3, 3]

【讨论】:

  • 我试过这个,但它似乎没有给我想要的东西(随机性不够)。也许我想要置换?
  • itertools 将以非随机顺序为您提供结果。你可以做什么:收集一组列表(获取所有组合,但没有重复),然后将它们打乱以随机顺序获取它们。或者你说的“我都想要”和“我需要随机化”的组合是什么意思?
  • 我基本上是在尝试在 32 个空格中找到 [0-9][a-f] 的组合,但我对 aaaaaaaaaaaaaaaaaaaaaaaaa 等不感兴趣。计算所有组合需要一生甚至更长的时间,所以我想我可以更聪明一点,并将其限制为每个项目出现 3 次。
  • 您期望的示例(对于较小的输入集)可能会有所帮助-正如您上面提到的,您可能真的在寻找排列而不是组合,或者您想要组合,但不是排序顺序,或...
  • 我刚刚在 7 个地方尝试了 [0-9a-f],它已经给出了 157488 种组合(对于我上面的算法)。对于 32 个地方,它仍然需要一生 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多