简体   繁体   中英

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

I'm new to python and I'm running into an issue in my project. I have to read a file containing users + tasks. Then I should list the user names, and count the number of name were listed in the file.. grouped together. Then once I have the count, calculate the percentage of that count with the number of users listed.

file contents look like this:

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

Here is my code -

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)

*I would like the results to look like this: user1: 3, 50% user4: 2, 33% user2: 1, 16.7%

If anyone has any suggestions, please let me know

code:

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)

You could try this:

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

The original way:

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

Or the faster way is to use 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)}%')

You could simply store all tasks of one user into a dictionary, using a list as value to append each incoming taks.

The amount of tasks per user is just the lenght of that list - all tasks are the sum of all lenghts:

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 %

While there is a lot of merit learning how to use the basic python types, the big benefit of python from my point of view is the vast array of libraries available that solve a large number of common problems already.

If you are going to find yourself managing and transforming data files frequently in this project, consider using a library.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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