如何创建一个字典来统计两个文件中值(用户名)的出现次数? – Python

huangapple go评论50阅读模式
英文:

How to create a dictionary to count value (username) appearances in two files? - Python

问题

尝试完成我课程的一个任务;

我有两个文件,我需要从中读取并比较以获取一个计数。
一个文件包含登录信息的格式如下:
用户名,密码

另一个文件包含任务的格式如下:
用户名,任务名称,任务描述,记录日期,截止日期,是否已完成?(Y/N)

我需要读取这两个文件并计算登录信息文件中的用户在任务文件中有多少任务。

到目前为止,我的代码如下:

user_file = open("user.txt", "r")
user_tasks = open("tasks.txt", "r")

num_users = user_file.readlines()
total_num_users = len(num_users) # 这是任务的另一部分

task_lines = user_tasks.readlines()
my_dict = {}

# TODO 使用 all_file_count 获取任务的总数

for user, task in zip(num_users, task_lines):
    user = user.strip("\n").split(", ")
    task = task.strip("\n").split(", ")
    if user[0] == task[0]:
        my_dict = dict(zip(user, task))
print(my_dict)

最初我只是尝试测试是否可以使其工作并显示用户和任务,但我不确定如何将其转化为计数,尽管我可以看到我的第一个障碍是上面的部分不起作用,因为我没有所有出现在两个文件中的用户和他们的任务。

这是输出:

{ 'admin': 'admin', 'adm1n': 'Register Users with taskManager.py' }

供参考:

我已经附上了txt文件的屏幕截图。

请注意:这不是真实数据,我没有分享人们的登录密码 如何创建一个字典来统计两个文件中值(用户名)的出现次数? – Python

英文:

trying to complete a task for my course;

I have two files that I need to read from and compare to get a count.
One file contains login information in the format of:
username, password

The other file has the tasks in the format of:
username, task name, task descrip, date logged, due date, is completed?(Y/N)

I need to read both files and count how many tasks a user in the login information file has in the task file.

This is what I have so far:

user_file = open("user.txt", "r")
user_tasks = open("tasks.txt", "r")

num_users = user_file.readlines()
total_num_users = len(num_users) # This is for a separate part of the task

task_lines = user_tasks.readlines()
my_dict = {}

# TODO use all_file_count for total number of tasks

for user, task in zip(num_users, task_lines):
    user = user.strip("\n").split(", ")
    task = task.strip("\n").split(", ")
    if user[0] == task[0]:
        my_dict = dict(zip(user, task))
print(my_dict)

Initially I was just trying to test if I could make it work and display the user and task but I am not sure how to transform it into a count, although I can see that my first hurdle is the above isn't working correctly as I don't have all my users and their tasks that appear in both files.

This is the output:

/Users/Joekelly/PycharmProjects/HyperionDev/venv/bin/python /Users/Joekelly/PycharmProjects/HyperionDev/main.py
{'admin': 'admin', 'adm1n': 'Register Users with taskManager.py'}

For reference:

I have attached screenshots of the txt. files
enter image description here
Please note: This is not real data I am not sharing people's login/passwords 如何创建一个字典来统计两个文件中值(用户名)的出现次数? – Python

答案1

得分: 1

步骤 1. 改进你的变量名称

最重要的是,在你的代码中,“num_users” 不是用户的数量,这会让读者感到困惑。

步骤 2. 不要尝试将列表一一对应

只有在两个文件完全平行的情况下才能起作用,也就是说,一个文件中的第4行直接对应另一个文件中的第4行。我认为这对你来说不适用,因为它们是独立的列表,只共享“user”的值。

步骤 3. 分三个阶段建立你的字典

首先,创建一个空字典,就像你已经做的那样。

然后,在用户文件中为每个用户添加一个空列表。

然后,遍历任务文件中的每一行,找出它属于哪个用户,并将其添加到该用户的任务列表中。

users_file = open("user.txt", "r")
user_lines = user_file.readlines()

tasks_file = open("tasks.txt", "r")
task_lines = user_tasks.readlines()

tasks_by_user = {}
for user_line in user_lines:
    user = user_line.strip("\n").split(", ")[0] 
    tasks_by_user[user] = 0

for task_line in task_lines:
    user = task_line.strip("\n").split(", ")[0]  
    if user in tasks_by_user:
        tasks_by_user[user] += 1
    else: 
        print("Omitting task for a user not in the users list:", task_line)

print(tasks_by_user)
英文:

Step 1. Improve your variable names

Most importantly, in your code "num_users" is NOT the number of users, which readers will find confusing.

Step 2. Don't try to zip the lists together

That would only work if the two files were exactly parallel, i.e. the #4 line in one file directly corresponds to the #4 line of the other. I think this is not the case for you, because they are separate lists that only share the value of "user".

Step 3. Build up your dictionary in three stages

First, an empty dictionary, as you have already done.

Then, add an empty list for each user in the user file.

Then, loop through each line in the tasks file, find out which user it belongs to, and add it to the list of tasks of that user.

users_file = open("user.txt", "r")
user_lines = user_file.readlines()

tasks_file = open("tasks.txt", "r")
task_lines = user_tasks.readlines()

tasks_by_user = {}
for user_line in user_lines:
    user = user_line.strip("\n").split(", ")[0] 
    tasks_by_user[user] = 0

for task_line in task_lines:
    user = task_line.strip("\n").split(", ")[0]  
    if user in tasks_by_user:
        tasks_by_user[user] += 1
    else: 
        print("Omitting task for a user not in the users list:", task_line)

print(tasks_by_user)

huangapple
  • 本文由 发表于 2023年2月26日 20:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定