+ 1

How to get average scores from different strings in one list?

Need to sort-out and find average scores. Input: ["Mike|83, 90, 34, 54", "Jane|45, 46, 53, 23"] Output: ["Mike|65", "Jane|42"] Thanks!

28th Feb 2020, 10:52 AM
Stanislav Voloshchuk
Stanislav Voloshchuk - avatar
6 odpowiedzi
+ 2
text = ["Mike|83, 90, 34, 54", "Jane|45, 46, 53, 23"] result = [] for record in text: cols = record.split("|") nums = [int(i) for i in cols[1].split(", ")] result.append("|".join([cols[0], str(round(sum(nums) / len(nums)))])) print(result)
28th Feb 2020, 12:28 PM
andriy kan
andriy kan - avatar
+ 4
Regular procedure in the forum is, that people who ask questions also show their attempt, before they get help in coding. What you can do is, first to replace "|" with ",", then do a regular split(','). Then use first item as name, and the other items to convert from string to int. Then use the numeric items to calculate the average.
28th Feb 2020, 1:04 PM
Lothar
Lothar - avatar
+ 3
Btw input is not very nice to parse. If possible(maybe not) I would change to either Mike 83 90 .... or if u have it as document to json Json can be easily parsed by almost all languages
28th Feb 2020, 12:41 PM
Oma Falk
Oma Falk - avatar
+ 3
Thank you everybody! ;) Especially andriy kan ! Lothar thank you for advice regarding regular procedures on the forum, but I thought by showing my attempts it could give wrong idea.
28th Feb 2020, 5:06 PM
Stanislav Voloshchuk
Stanislav Voloshchuk - avatar
+ 2
Thanks for reply Seb TheS but "Page Not Found"
28th Feb 2020, 12:27 PM
Stanislav Voloshchuk
Stanislav Voloshchuk - avatar
0
String split method would make this very easy to solve: https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2456/
28th Feb 2020, 11:29 AM
Seb TheS
Seb TheS - avatar