创建多项选择测验答案组合列表(具有特定条件)的内存有效方式?
Memory-efficient way to create a list of multiple choice quiz answer combinations (with specific conditions)?
我正在尝试创建多项选择测验的所有可能答案组合的列表。测验中有 30 个问题,有 4 个可能的答案:['agree', 'disagree', 'neither', 'skip']
。输出应该是一个子列表列表,其中包含 30 个问题的所有可能答案组合,例如:
[['agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree'], ['agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'disagree'], etc. ...]
有一个 警告 :skip
最多只能回答。每个测验 15 个问题。
看起来itertools.product
是最直接的解决方案,所以我尝试了:
from itertools import product
combinations = product(['agree', 'disagree', 'neither', 'skip'], repeat=30)
但是,这会非常低效地占用内存,并且会不断使我的 Colab 会话崩溃。是否有一种内存高效的解决方案实际上可以让我生成测验答案的组合,最重要的是:我怎样才能实现每个测验最多 15 skip
个答案?
您在使用 python2 吗?
Python3 的 itertool.product 不应该让你的 RAM 爆炸。它应该只是一个发电机。
您的代码应该可以正常工作,但是它会 运行 相当长一段时间(正如其他人指出的 'quite some time' 意味着您不想等待计算的时间你电脑的能力。如果我没算错,如果你的电脑每秒能处理100万个组合,你得等大约36000年)
In [21]: 4**30 / 10**6 / 60 / 60 / 24 / 365
Out[21]: 36558.901084692
请 运行 下面的代码加上 python 3:
choices = ['agree', 'disagree', 'neither', 'skip']
for i, answer in enumerate(itertools.product(choices, repeat=30)):
pass
print(i)
使用您的进程监视器,您应该会看到,您的进程不需要
很多内存,但只有很多 CPU.
仅具有最多 15 个跳过值的答案的蛮力解决方案是获取所有可能性并拒绝具有更多跳过值的答案。
另一个方法是首先创建所有答案,仅包含
['agree', 'disagree', 'neither']
,
然后所有答案正好为 1 'skip'
,并在所有可能的位置跳过
然后所有的答案正好是 2 'skip'
s,在所有可能无法区分的位置都会跳过。
...
我正在尝试创建多项选择测验的所有可能答案组合的列表。测验中有 30 个问题,有 4 个可能的答案:['agree', 'disagree', 'neither', 'skip']
。输出应该是一个子列表列表,其中包含 30 个问题的所有可能答案组合,例如:
[['agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree'], ['agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'agree', 'disagree'], etc. ...]
有一个 警告 :skip
最多只能回答。每个测验 15 个问题。
看起来itertools.product
是最直接的解决方案,所以我尝试了:
from itertools import product
combinations = product(['agree', 'disagree', 'neither', 'skip'], repeat=30)
但是,这会非常低效地占用内存,并且会不断使我的 Colab 会话崩溃。是否有一种内存高效的解决方案实际上可以让我生成测验答案的组合,最重要的是:我怎样才能实现每个测验最多 15 skip
个答案?
您在使用 python2 吗?
Python3 的 itertool.product 不应该让你的 RAM 爆炸。它应该只是一个发电机。
您的代码应该可以正常工作,但是它会 运行 相当长一段时间(正如其他人指出的 'quite some time' 意味着您不想等待计算的时间你电脑的能力。如果我没算错,如果你的电脑每秒能处理100万个组合,你得等大约36000年)
In [21]: 4**30 / 10**6 / 60 / 60 / 24 / 365
Out[21]: 36558.901084692
请 运行 下面的代码加上 python 3:
choices = ['agree', 'disagree', 'neither', 'skip']
for i, answer in enumerate(itertools.product(choices, repeat=30)):
pass
print(i)
使用您的进程监视器,您应该会看到,您的进程不需要 很多内存,但只有很多 CPU.
仅具有最多 15 个跳过值的答案的蛮力解决方案是获取所有可能性并拒绝具有更多跳过值的答案。
另一个方法是首先创建所有答案,仅包含
['agree', 'disagree', 'neither']
,
然后所有答案正好为 1 'skip'
,并在所有可能的位置跳过
然后所有的答案正好是 2 'skip'
s,在所有可能无法区分的位置都会跳过。
...