+ 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!
6 Réponses
+ 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)
+ 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.
+ 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
+ 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.
+ 2
Thanks for reply Seb TheS but "Page Not Found"
0
String split method would make this very easy to solve:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2456/