我正在使用Python并考虑以下问题:给定一个列表,例如[1, 0, -2, 0, 0, 4, 5, 0, 3]多次包含整数0 的列表,我希望索引位于这些0和每一个,它出现在列表中的次数直到出现不同的元素或列表结束.
给定l = [1, 0, -2, 0, 0, 4, 5, 0],函数将返回((1, 1), (3, 2), (7, 1)).结果是一个元组列表.元组的第一个元素是给定元素的索引(在列表中),第二个元素是在不同元素出现或列表结束之前重复的次数.
天真地,我会写这样的东西:
def myfun(l, x):
if x not in l:
print("The given element is not in list.")
else:
j = 0
n = len(l)
r = list()
while j <= (n-2):
count = 0
if l[j] == x:
while l[j + count] == x and j <= (n-1):
count +=1 …Run Code Online (Sandbox Code Playgroud) 我使用Elixir和Phoenix开发了像素跟踪微服务.我正在尝试获取安装像素跟踪的原始URL Plug.Conn.
我假设我可以尝试获取Plug.Conn的HTTP_REFERRER标头或变量或者其他东西,但我可能错了,可能浏览器和HTTP如何工作,因为我Plug.Conn在控制器中找不到关于referrer的任何内容.
有任何想法吗?
我有以下集合:
error_reports
[
{
"_id":{
"$oid":"5184de1261"
},
"date":"29/04/2013",
"errors":[
{
"_id":"10",
"failures":2,
"alerts":1,
},
{
"_id":"11",
"failures":7,
"alerts":4,
}
]
},
{
"_id":{
"$oid":"5184de1262"
},
"date":"30/04/2013",
"errors":[
{
"_id":"15",
"failures":3,
"alerts":2,
},
{
"_id":"16",
"failures":9,
"alerts":1,
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
是否可以检索出现故障的文档列表,并按故障降序排序警报总和?我是mongodb的新手,我已经搜索了2天,但我无法弄清楚什么是正确的查询......
我试过这样的事情:
db.error_reports.aggregate(
{ $sort : { failures: -1} },
{ $group:
{ _id: "$_id",
failures: { "$sum": "$errors.failures" }
}
}
);
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我认为是因为$sum:$errors.failures事情,我想在day_hours子集合的每个项目上总结这个属性,但我不知道在查询中这样做...