繁体   English   中英

如何找到列表中元素的百分比? (Python)

[英]How do i find the percentage of the elements in a list? (Python)

我是 python 的新手,我在项目中遇到了问题。 我必须阅读一个包含用户+任务的文件。 然后我应该列出用户名,并计算文件中列出的名称的数量..分组在一起。 然后,一旦我有了计数,就用列出的用户数计算该计数的百分比。

文件内容如下所示:

user1, task
user2, task
user1, task
user4, task
user4, task
user1, task

这是我的代码 -

with open('tasks.txt', 'r') as tasks:
    for line in tasks.readlines():
        mine = line.lower().split(", ")
        for i in mine[0].split(", "):
            cnt[i] += 1
    print("\nThese are the number of tasks assigned to each user: \n" + str(cnt))
    t = sum(cnt.values())
    d = dict(cnt)
    u, v = zip(*d.items())
    print(u, v)
    for n in v:
        divide = float(n / t) * 100
        print("The users are assigned this percentage of the tasks: \n")
        print(n, divide)

*我希望结果如下所示:user1: 3, 50% user4: 2, 33% user2: 1, 16.7%

如果有人有任何建议,请告诉我

代码:

cnt={}
usertask = []
res = {}
with open('task.txt', 'r') as tasks:
    for line in tasks.readlines():
        mine = line.lower().split(", ")
        usertask.append(mine[0])

for i in (list(set(usertask))):
    cnt[i]=0

for user in usertask:
    cnt[user]+=1

for user,task in cnt.items():
    res[user]=task*(100/len(usertask))

print(res)

你可以试试这个:

# read data to a list
with open('tasks.txt', 'r') as f:
    lines = f.readlines()
    lines = [line.strip() for line in lines]

原来的方式:

from collections import defaultdict
count = defaultdict(list)
for line in lines:
    user, task = line.split(', ')
    count[user].append(task)
for user, tasks in count.items():
    print(f'{user}: {len(tasks)*100/len(lines)}%')

或者更快的方法是使用Counter

from collections import Counter
users = [line.split(', ')[0] for line in lines]
count = Counter(users)
for user, value in count.items():
    print(f'{user}: {value*100/len(lines)}%')

您可以简单地将一个用户的所有任务存储到字典中,使用list作为 append 每个传入任务的值。

每个用户的任务数量只是该列表的长度 - 所有任务都是所有长度的总和:

fn = "d.txt"

# write demo data
with open (fn,"w") as f:
    f.write("""user1, task
user2, task
user1, task
user4, task
user4, task
user1, task""")

from collections import defaultdict

# use a dicts with values that default to list
users=defaultdict(list)

with open(fn) as tasks:
    for line in tasks:
        # split your line into 2 parts at 1st ',' - use 1st as user, 2nd as task-text
        user, task = line.strip().lower().split(", ",1)

        # append task to user, autocreates key if needed
        users[user].append(task)

    # sum all lenght values together
    total_tasks = sum(map(len,users.values()))

    # how much % equals one assigned task?
    percent_per_task = 100 / total_tasks

    for user, t in users.items():
        # output stuff
        lt = len(t)
        print(user, lt, (lt * percent_per_task),'%')

Output:

user1 3 50.0 %
user2 1 16.666666666666668 %
user4 2 33.333333333333336 %

虽然学习如何使用基本的 python 类型有很多优点,但从我的角度来看,python 的最大好处是已经解决了大量常见问题的大量可用库。

如果您发现自己在此项目中经常管理和转换数据文件,请考虑使用库。

import pandas   #import the pandas library
df = pandas.read_csv('tasks.txt', header=None, names=['user', 'task']) #read you file into a dataframe, which is a table like object
df['user'].value_counts(normalize=True).mul(100) #count the number of users, where the parameter normalize gives each count as a fraction, then mul (short for multiply) by 100 to turn the fraction into a percentage.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM