0

我是 Python 新手,所以请多多包涵。

当我尝试打印它们时,我有一个名为FUNNEL_STEP_ARRAY返回值的数组。i但是,我想for (i)在每个循环中返回查询结果。

for i in FUNNEL_STEP_ARRAY:
    print (i) #prints the step name
    y = db.external_funnel.find(
    {"step_id":"\i", "client_timestamp": {"$regex": "2013-10-25T"},
    "application_id":"6083502215600857441"
    }).distinct("user_id")
    print(len(y))

当我打印数组中最后一步的名称时,我得到了适当的响应,但是我无法找出让我输入上述每个循环的查询的适当方法。

y = db.external_funnel.find(
{"step_id":"Tutorial.TAttackScreen.End", "client_timestamp": {"$regex": "2013-10-25T"},
"application_id":"6083502215600857441"
}).distinct("user_id")

print(y) ['11856662934482915056', '6822304154484365317', '16907601980584545477', '7754468465932591739', '4035769163970893047', '1722852941393395710', '16289507963782337913', '12342123419828328163', '15183373460796791586', '3923264265959269816', '8842662517796204109', ' 1330883712013359625', '6130265032042700646', '3280282641258166143', '18002035489108466380', '8584675150263264823', '13183853476835939343', '12332104951386419645', '3477173628398869287', '8625222330019018139', '1937123521362305652', '11430291986398375763', '8051074243207166456', '17635653501888308620' ] 打印(len(y)) 24

理想情况下,我正在寻找每个 (i) 的输出,如下所示:

Tutorial.TAtackScreen.End 24

4

1 回答 1

0

目前,在 for 循环中,您正在执行"step_id": "\i". 这实际上是在说您希望步骤 id 是字符“反斜杠 i”。相反,您想通过简单地执行以下操作来使用变量的内容:

for i in FUNNEL_STEP_ARRAY:
    y = db.external_funnel.find({
        "step_id": i, 
        "client_timestamp": {"$regex": "2013-10-25T"},
        "application_id":"6083502215600857441"
    }).distinct("user_id")
    print(len(y))

i注意:为变量使用更明确的名称可能会更好。例如:

for step_id in FUNNEL_STEP_ARRAY:
    y = db.external_funnel.find({
        "step_id": step_id, 
        "client_timestamp": {"$regex": "2013-10-25T"},
        "application_id":"6083502215600857441"
    }).distinct("user_id")
    print(len(y))
于 2013-10-29T17:37:09.583 回答