如何准确统计tasks.txt中每个用户的任务分配数量
如何准确统计每个用户的任务分配数量
问题说明
需要读取tasks.txt文件(记录各用户的任务),和username_storage列表比对,统计并输出每个用户的任务数量。下面是两次失败的尝试、结果,以及正确的解决方法。
第一次尝试代码
tasks_for_user = 0 username_storage = ['admin', 'banta', 'david,', 'hyperionDev', 'rebeca'] tasks = open("tasks.txt", "r") i = 0 for task in tasks: split = task.strip().split(", ") while i < len(username_storage): if username_storage[i] == split[0]: tasks_for_user += 1 print(f"There are {tasks_for_user} tasks assigned to {username_storage[i]}") i += 1 else: tasks_for_user = 0 print(f"There are {tasks_for_user} tasks assigned to {username_storage[i]}") i += 1
输出结果:
There are 1 tasks assigned to admin There are 0 tasks assigned to banta There are 0 tasks assigned to david, There are 0 tasks assigned to hyperionDev There are 0 tasks assigned to rebeca
问题所在:逻辑完全错误——只处理了第一条任务就遍历完所有用户,后续任务根本没机会匹配;而且只要不匹配就重置计数,完全达不到统计所有对应任务的目的。
第二次尝试代码
for users_ in username_storage: for task in tasks: split = task.strip().split(", ") if users_ == split[0]: tasks_for_user += 1 else: tasks_for_user += 0 print(f"There are {tasks_for_user} tasks assigned to {users_}")
输出结果:
There are 2 tasks assigned to admin There are 2 tasks assigned to banta There are 2 tasks assigned to david, There are 2 tasks assigned to hyperionDev There are 2 tasks assigned to rebeca
问题所在:文件对象是迭代器,第一次循环admin时已读完所有任务,后续用户循环时tasks无内容;且tasks_for_user未在每个用户循环前重置,导致所有用户继承第一个用户的计数。
tasks.txt内容
admin, Register Users with taskManager.py, Use taskManager.py to add the usernames and passwords for all team members that will be using this program., 10 Oct 2019, 20 Oct 2019, No admin, Assign initial tasks, Use taskManager.py to assign each team member with appropriate tasks, 10 Oct 2019, 25 Oct 2019, No banta, FCB, This task is to find new young talents for futbol club barcelona, 20 Jan 2023, 2023-01-07, Yes banta, Programming, Why do you love to program, 31 Jan 2023, 2023-01-08, No
正确解法
推荐方法:用字典先统计所有任务的用户计数
这种方法仅读取一次文件,效率高、逻辑清晰:
username_storage = ['admin', 'banta', 'david,', 'hyperionDev', 'rebeca'] # 初始化每个用户的任务计数为0 task_counts = {user: 0 for user in username_storage} # 读取文件并统计 with open("tasks.txt", "r") as tasks_file: for line in tasks_file: line = line.strip() if not line: # 跳过空行 continue # 拆分出任务对应的用户 task_user = line.split(", ")[0] # 如果用户在我们的列表里,计数加1 if task_user in task_counts: task_counts[task_user] += 1 # 输出每个用户的任务数 for user, count in task_counts.items(): print(f"There are {count} tasks assigned to {user}")
输出结果:
There are 2 tasks assigned to admin There are 2 tasks assigned to banta There are 0 tasks assigned to david, There are 0 tasks assigned to hyperionDev There are 0 tasks assigned to rebeca
备选方法:每次循环用户时重新读取文件(效率低,不推荐)
如果不想用字典,可每个用户循环时重新打开文件读取,逻辑简单但性能较差:
username_storage = ['admin', 'banta', 'david,', 'hyperionDev', 'rebeca'] for user in username_storage: count = 0 with open("tasks.txt", "r") as tasks_file: for line in tasks_file: line = line.strip() if not line: continue task_user = line.split(", ")[0] if task_user == user: count += 1 print(f"There are {count} tasks assigned to {user}")
核心注意点
- 用字典初始化所有用户的计数,确保每个用户都能被统计到,哪怕没有任务。
- 用
with语句操作文件,自动处理关闭,避免资源泄漏。 - 跳过空行,防止处理无效内容导致错误。
- 优先使用第一种方法,仅读取一次文件,性能更优。
内容的提问来源于stack exchange,提问作者Banta Jabbi
相关产品推荐
相关产品推荐

