簡體   English   中英

創建多個列表的所有可能組合以創建調查路由邏輯

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

我目前編寫了很多使用 XML 的調查腳本,您可以在其中創建路由邏輯、執行塊等。我知道/使用一些非常基本的 python,我正在嘗試了解更多可以應用於我的調查的信息。

在我的調查開始時,我收集客戶細分信息,例如年齡、位置、社會等級等。然后將這些信息組合起來用於配額目的。

這些保存在具有以下類別的自動填充中(IL 代表互鎖)

IL_性別
r1 : 男性
r2 : 女
r3 : 其他

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

IL_Region
r1 : 北
r2 : 南
r3 :東r4 :西

IL_SEG
r1 : ABC1
r2 : C2DE

從這些自動填充中,我想使用以下語法創建這些自動填充的所有可能組合(在本例中為 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>

等等...

我不是要人們為我做這件事我只是想知道這會有多困難,如果有人能給我指出正確的方向,告訴我要使用什么功能/我可以采取的步驟以及任何可能幫助我學習的資源這該怎么做。

最終這實際上取決於您的客戶細分信息的存儲方式。 假設它是字典形式。 然后你可以使用itertools.product function 來獲得你想要的一切,但你還需要做一些預處理和一些后處理以將它以所需的形式存儲在 html 中。 如果你有一個模板庫可以為你做這件事(你的 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])

在這里,我使用 Python 的內置 模板字符串,它需要{}在模板字符串中使用帶有尾隨_的鍵,但在模板字符串的早期實例中不需要

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM