我想在办公室里找到一天中最忙碌的时刻 - python

我有一个列表a = [(1,3),(3,7),(1,10),(3,5),......]等等。即(进入时间,退出时间)。

其中每个元组的第一个元素是雇员的入职时间,第二个元素是离职时间。需要找到时间,例如一天中的哪个小时办公室里最多的人。

例如输出:

{1'00: 10, 2'00: 20, 3'00: 15}

因此最终输出应为2'00,计数为20。

参考方案

将列表理解与flatten和range一起使用,然后使用collections.Counter并最后提取最大值:

a = [(1, 3), (3, 7), (1, 10), (3, 5)]

from collections import Counter

d = Counter([f'{y}:00' for s, e in a for y in range(s, e + 1)])
print(d)
Counter({'3:00': 4, '4:00': 3, '5:00': 3, '1:00': 2, '2:00': 2,
     '6:00': 2, '7:00': 2, '8:00': 1, '9:00': 1, '10:00': 1})

maximum = max(d, key=d.get)
print(maximum, d[maximum])

3:00 4

如果不计算元组的最后一个值:

d = Counter([f'{y}:00' for s, e in a for y in range(s, e)])
print (d)
Counter({'3:00': 3, '4:00': 3, '1:00': 2, '2:00': 2,
         '5:00': 2, '6:00': 2, '7:00': 1, '8:00': 1, '9:00': 1})

maximum = max(d, key=d.get)
print(maximum, d[maximum])
3:00 3

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

python JSON对象必须是str,bytes或bytearray,而不是'dict - python

在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python:使用两个列表进行字典 - python

如何使用python使用两个列表作为字典list_one_keys = ['key1', 'key2', 'key3', 'key4'] 嵌套列表:list_two_values = [['a1var1', 'a1var2', '…