简体   繁体   English

在 Itertools 产品期间从组合中删除项目

[英]Remove Items from Combination during Itertools Product

I know this has been asked before, but I can't find a solution that fits my problem:我知道之前有人问过这个问题,但我找不到适合我问题的解决方案:

  • I have a list of people that can appear in six slots (no repeats).我有一个可以出现在六个位置的人的列表(没有重复)。
  • I use itertools.product to iterate through them and assign each to a slot.我使用 itertools.product 遍历它们并将每个分配给一个插槽。 I then skip any combination that does not fit my criteria.然后我会跳过任何不符合我标准的组合。
  • To speed it up (it takes forever), I try to pre-trim the lists of people.为了加快速度(这需要很长时间),我尝试预先整理人员名单。
  • To further speed it up (this is the key), I want to fully drop people from the list over which I am iterating so that it never has to consider that person again.为了进一步加快速度(这是关键),我想将人员从我正在迭代的列表中完全删除,这样它就不必再考虑那个人了。 How do I do that?我怎么做?

Current Code:当前代码:

for combination in itertools.product(nameList1,nameList2,nameList3,nameList4,nameList5,nameList6):
        nameofPlayer1 = combination[0]
        nameofPlayer2 = combination[1]
        etc.

Then:然后:

if nameofPlayer1 == nameofPlayer2:
            continue
        if nameofPlayer1 == nameofPlayer3:
            continue
        etc.

What I want to do now is:我现在想做的是:

if timesUsedPlayer1 > xyz:
        #remove Player1 from nameList1, nameList2, etc.

I believe this would speed up the subsequent iterations since, even though the combination would ultimately fail (because I have other triggers for checking how many times someone was used), I can cut out the "check" entirely by having the person excluded from the iteration.我相信这会加速后续迭代,因为即使组合最终会失败(因为我有其他触发器来检查某人被使用了多少次),我可以通过将此人排除在外来完全切断“检查”迭代。

Thanks in advance!提前致谢!

Here is code that shows a way to use permutations() in itertools to generate all possible sequences of 6 names from the list of people mentioned in the question such that a name may get dropped and no longer considered for processing.下面的代码显示了一种在itertools中使用permutations()从问题中提到的人员列表中生成所有可能的 6 个名称序列的方法,这样一个名称可能会被删除并且不再考虑处理。

numPrints = 0
nameList = [
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"
]
dropped = set()
for combination in itertools.permutations(nameList, 6):
    for name in combination:
        # this simulates the condition that would lead to removal of a name from further consideration
        if name in ["b", "c", "d", "e", "f"]:
            dropped.add(name)
    combSet = set(combination)
    if not combSet.isdisjoint(dropped):
        # the combination contains a name that has been dropped, so ignore it
        continue
    # process the combination here
    if numPrints < 10:
        print(f"processing {combination}")
        numPrints += 1
        if numPrints == 10:
            print("etc.")

Output for the above example:上例为 Output:

processing ('a', 'g', 'h', 'i', 'j', 'k')
processing ('a', 'g', 'h', 'i', 'k', 'j')
processing ('a', 'g', 'h', 'j', 'i', 'k')
processing ('a', 'g', 'h', 'j', 'k', 'i')
processing ('a', 'g', 'h', 'k', 'i', 'j')
processing ('a', 'g', 'h', 'k', 'j', 'i')
processing ('a', 'g', 'i', 'h', 'j', 'k')
processing ('a', 'g', 'i', 'h', 'k', 'j')
processing ('a', 'g', 'i', 'j', 'h', 'k')
processing ('a', 'g', 'i', 'j', 'k', 'h')
etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM