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

时间:2020-04-17 07:04:21

标签: python iteration counter

我是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)

*我希望结果看起来像这样: 用户1:3,50% 用户4:2,33% 用户2:1,16.7%

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

4 个答案:

答案 0 :(得分:2)

代码:

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)

答案 1 :(得分:1)

您可以尝试以下方法:

# 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)}%')

答案 2 :(得分:0)

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

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

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),'%')

输出:

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

答案 3 :(得分:0)

虽然学习如何使用基本的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.