简体   繁体   English

创建多个列表的所有可能组合以创建调查路由逻辑

[英]Creating all possible combinations of multiple lists to create survey routing logic

I currently script a lot of surveys that use XML where you can create routing logic, exec blocks etc. I know / use some very basic python and I am trying to learn more that I can apply to my surveys.我目前编写了很多使用 XML 的调查脚本,您可以在其中创建路由逻辑、执行块等。我知道/使用一些非常基本的 python,我正在尝试了解更多可以应用于我的调查的信息。

At the start of my surveys I gather customer segmentation info such as Age, Location, Social grade etc. These are then combined for quota purposes.在我的调查开始时,我收集客户细分信息,例如年龄、位置、社会等级等。然后将这些信息组合起来用于配额目的。

These are saved in autofills with the following categories (IL stands for interlock)这些保存在具有以下类别的自动填充中(IL 代表互锁)

IL_Gender IL_性别
r1 : Male r1 : 男性
r2 : Female r2 : 女
r3 : Other r3 : 其他

IL_Age IL_Age
r1 : 18-34 r1 : 18-34
r2 : 35-54 r2 :35-54
r3 : 55plus r3 :55plus

IL_Region IL_Region
r1 : North r1 : 北
r2 : South r2 : 南
r3 : East r4 : West r3 :东r4 :西

IL_SEG IL_SEG
r1 : ABC1 r1 : ABC1
r2 : C2DE r2 : C2DE

From these autofills I would like to create all of the possible combinations (in this case 72) of these using the following syntax:从这些自动填充中,我想使用以下语法创建这些自动填充的所有可能组合(在本例中为 72):

<row label="r1" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r1) and (IL_SEG.r1)">1_Male_Age_1834_North_ABC1</row>
<row label="r2" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r1) and (IL_SEG.r2)">2_Male_Age_1834_North_C2DE</row>
<row label="r3" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r2) and (IL_SEG.r1)">3_Male_Age_1834_South_ABC1</row>
<row label="r4" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r2) and (IL_SEG.r2)">4_Male_Age_1834_South_C2DE</row>
<row label="r5" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r3) and (IL_SEG.r1)">5_Male_Age_1834_East_ABC1</row>
<row label="r6" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r3) and (IL_SEG.r2)">6_Male_Age_1834_East_C2DE</row>
<row label="r7" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r4) and (IL_SEG.r1)">7_Male_Age_1834_West_ABC1</row>
<row label="r8" autofill="(IL_Gender.r1) and (IL_Age.r1) and (IL_Region.r4) and (IL_SEG.r2)">8_Male_Age_1834_West_C2DE</row>
<row label="r9" autofill="(IL_Gender.r1) and (IL_Age.r2) and (IL_Region.r1) and (IL_SEG.r1)">9_Male_Age_3554_North_ABC1</row>
<row label="r10" autofill="(IL_Gender.r1) and (IL_Age.r2) and (IL_Region.r1) and (IL_SEG.r2)">10_Male_Age_3554_North_C2DE</row>
<row label="r11" autofill="(IL_Gender.r1) and (IL_Age.r2) and (IL_Region.r2) and (IL_SEG.r1)">11_Male_Age_3554_South_ABC1</row>
<row label="r12" autofill="(IL_Gender.r1) and (IL_Age.r2) and (IL_Region.r2) and (IL_SEG.r2)">12_Male_Age_3554_South_C2DE</row>

And so on ...等等...

I'm not asking for people to do this for me I'm just wondering how difficult this would be and if anyone could point me in the right direction on what functions to use / steps I can take and any resources that might help me learn how to do this.我不是要人们为我做这件事我只是想知道这会有多困难,如果有人能给我指出正确的方向,告诉我要使用什么功能/我可以采取的步骤以及任何可能帮助我学习的资源这该怎么做。

Ultimately this really depends on how your customer segmentation info is stored.最终这实际上取决于您的客户细分信息的存储方式。 Let's assume it's in dictionary form.假设它是字典形式。 Then you can use the itertools.product function to get everything you want but you also need to do a bit of preprocessing and some postprocessing to store it all in the html in the desired form.然后你可以使用itertools.product function 来获得你想要的一切,但你还需要做一些预处理和一些后处理以将它以所需的形式存储在 html 中。 If you have a template library that will do this for you (your web framework may have something that is preferable. Ultimately this really depends on how you want to use the output. Here's one way:如果你有一个模板库可以为你做这件事(你的 web 框架可能有一些更好的东西。最终这真的取决于你想如何使用 output。这是一种方法:

from itertools import product
from string import Template


oneDict = {"IL_Gender":{"r1": "Male", "r2": "Female", "r3": "Other"}, 
           "IL_Age": {"r1": "18-34", "r2": "35-54", "r3": "55plus"},
           "IL_Region": {"r1": "North", "r2": "South", "r3": "East", "r4": "West"},
           "IL_SEG": {"r1": "ABC1", "r2": "C2DE"}
          }


# create list of lists of outerkey.innerkey
arrays = []
for key in oneDict.keys():
    arrays.append([key + '.' + val for val in oneDict[key].keys()])

table_rows = []
t=Template('<row label=\"r$label\" autofill=\"($gender) and ($age) and ($region) \
            and ($seg)\">${label}_${gender}_${age}_${region}_${seg}</row>')

for idx, tup in enumerate(product(*arrays)):
    d = dict(label=idx, gender=tup[0], age=tup[1], region=tup[2], seg=tup[3])
    table_rows.append(t.safe_substitute(d))

# show first and last row
print(table_rows[0])
print(table_rows[71])

Here I use Python's builtin Template strings which require the {} s around the use of the keys with trailing _ in the template string but are not required in the earlier instances in the templated string.在这里,我使用 Python 的内置 模板字符串,它需要{}在模板字符串中使用带有尾随_的键,但在模板字符串的早期实例中不需要

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

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