937135952 发表于 2021-10-19 18:43:09

怎么实现把两个列表联合起来统计

假设这么一个场景,name里记录每次访客名字,date里记录每位访客访问日期。每位访客多次来访只计入1,要统计每日访客人数(重复不算)
name = ["张三","李四","王五”","李四","张三","李四","王五”]
date = ["01-03","01-03","01-03","01-03","01-02","01-02","01-01"]

统计结果应该是:01-03:3人,01-02:两人,01-01:1人。
求出大佬们应该如何实现

937135952 发表于 2021-10-19 18:44:02

每位访客每日多次来访只计入1

傻眼貓咪 发表于 2021-10-19 18:52:58

date = ["01-03","01-03","01-03","01-03","01-02","01-02","01-01"]

for i in range(10):
    x = f"01-0{i}"
    if x in date:
      print(f"{x}:{date.count(x)}人")01-01:1人
01-02:2人
01-03:4人

kogawananari 发表于 2021-10-19 20:54:48

name列表好像完全没用到 只用到了date列表的次数记录吗

937135952 发表于 2021-10-19 22:24:21

傻眼貓咪 发表于 2021-10-19 18:52


不是的,01-03只能计3人,因为李四是当天重复来访

hrpzcf 发表于 2021-10-19 23:49:32

本帖最后由 hrpzcf 于 2021-10-20 00:11 编辑

name = ["张三", "李四", "王五", "李四", "张三", "李四", "王五"]
date = ["01-03", "01-03", "01-03", "01-03", "01-02", "01-02", "01-01"]

record = dict()
for d, n in zip(date, name):
    if d not in record:
      record =
    else:
      if n in record:
            continue
      record.append(n)
for d, i in record.items():
    print("{}: {}人".format(d, len(i)))
页: [1]
查看完整版本: 怎么实现把两个列表联合起来统计