[英]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:我知道之前有人问过这个问题,但我找不到适合我问题的解决方案:
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.