[英]Getting a sublist from a list and loops
So I have this variable that holds this.所以我有这个变量来保存这个。
['Needie Seagoon', 0.6, 0.8556701030927835, 0.5555555555555556, 0.8297872340425532, 0.978494623655914, 0.7849462365591398, 0.7142857142857143, 0.6436781609195402]
['Eccles', 0.98, 0.9381443298969072, 0.8080808080808081, 0.6947368421052632, 0.8850574712643678, 0.7959183673469388, 0.5161290322580645, 0.875]
['Bluebottle', 0.6421052631578947, 0.9072164948453608, 0.8080808080808081, 0.6382978723404256, 0.4838709677419355, 0.5591397849462365, 1.0, 0.9770114942528736]
['Henry Crun', 0.968421052631579, 0.5979381443298969, 0.5050505050505051, 0.6063829787234043, 0.7204301075268817, 0.4838709677419355, 0.8461538461538461, 0.8275862068965517]
['Minnie Bannister', 0.5368421052631579, 1.0, 0.5252525252525253, 0.5638297872340425, 0.7311827956989247, 0.6236559139784946, 0.7692307692307693, 0.7931034482758621]
['Hercules Grytpype-Thynne', 0.78, 0.6391752577319587, 0.7575757575757576, 0.7052631578947368, 0.5517241379310345, 0.5714285714285714, 0.956989247311828, 0.7613636363636364]
['Count Jim Moriarty', 0.5368421052631579, 0.7010309278350515, 0.5151515151515151, 0.7021276595744681, 0.5913978494623656, 0.7741935483870968, 0.5494505494505495, 0.8505747126436781]
['Major Dennis Bloodnok', 0.54, 0.4845360824742268, 0.5959595959595959, 0.5052631578947369, 0.7586206896551724, 0.5918367346938775, 0.5698924731182796, 0.9431818181818182]
Is it best to add these sublists into an array list so say new_li = [] and append the list to that?最好将这些子列表添加到数组列表中,比如 new_li = [] 并将列表附加到该列表中吗?
I want to loop through each name and their scores to get the mean average of all scores for that student.我想遍历每个名字和他们的分数,以获得该学生所有分数的平均平均值。 To loop through this easier I just spliced the list like so:
为了更容易地循环,我只是像这样拼接列表:
NOTE: Studentp_file is the list variable for above lists.注意:Studentp_file 是上述列表的列表变量。
for line in studentp_file:
answer = line[1:14]
The result is a new list that has only the numbers:结果是一个只有数字的新列表:
[0.6, 0.8556701030927835, 0.5555555555555556, 0.8297872340425532, 0.978494623655914, 0.7849462365591398, 0.7142857142857143, 0.6436781609195402]
[0.98, 0.9381443298969072, 0.8080808080808081, 0.6947368421052632, 0.8850574712643678, 0.7959183673469388, 0.5161290322580645, 0.875]
[0.6421052631578947, 0.9072164948453608, 0.8080808080808081, 0.6382978723404256, 0.4838709677419355, 0.5591397849462365, 1.0, 0.9770114942528736]
[0.968421052631579, 0.5979381443298969, 0.5050505050505051, 0.6063829787234043, 0.7204301075268817, 0.4838709677419355, 0.8461538461538461, 0.8275862068965517]
[0.5368421052631579, 1.0, 0.5252525252525253, 0.5638297872340425, 0.7311827956989247, 0.6236559139784946, 0.7692307692307693, 0.7931034482758621]
[0.78, 0.6391752577319587, 0.7575757575757576, 0.7052631578947368, 0.5517241379310345, 0.5714285714285714, 0.956989247311828, 0.7613636363636364]
[0.5368421052631579, 0.7010309278350515, 0.5151515151515151, 0.7021276595744681, 0.5913978494623656, 0.7741935483870968, 0.5494505494505495, 0.8505747126436781]
[0.54, 0.4845360824742268, 0.5959595959595959, 0.5052631578947369, 0.7586206896551724, 0.5918367346938775, 0.5698924731182796, 0.9431818181818182]
So I want to add up the values of say the first line and get the average.所以我想把第一行的值加起来并得到平均值。 Then do so for the second, third and so on.
然后对第二个,第三个等等这样做。
How would I loop this?我将如何循环这个? I can't seem to get the line by line loop going well.
我似乎无法让一行一行的循环顺利进行。
The plan is to add the names back in later.计划是稍后再添加名称。
Thanks.谢谢。
for sublist in list:
for value in sublist:
if not isinstance(value, float):
sublist.remove(value)
print(sublist)
I hope it will fullfill your requiremnets我希望它会满足您的要求
If we put all the lists
into one list
we can then loop through each sublist and calculate the mean
for each sliced sublist [1:]
this will skip the first item which is the name of the student如果我们将所有
lists
放入一个list
我们就可以遍历每个子列表并计算每个切片子列表的mean
[1:]
这将跳过第一项,即学生的姓名
List comprehension列表理解
means = [sum(i[1:])/float(len(i[1:])) for i in l]
[0.7453022035139001, 0.8116333563690437, 0.7519653356706918, 0.6944792261318251, 0.6928871681167221, 0.7154399707796905, 0.6525961084709853, 0.6236613189972134]
Expanded Loop:扩展循环:
means = []
for i in l:
means.append(sum(i[1:])/float(len(i[1:])))
You can use np.mean()
with a few list comprehensions and a dictionary comprehension to assemble your output:您可以将
np.mean()
与一些列表np.mean()
和字典np.mean()
一起使用来组合您的输出:
import numpy as np
students = [i[0] for i in data]
scores = [np.mean(np.array(i[1:])) for i in data]
answer = {student: score for student, score in zip(students, scores)}
Yields:产量:
{'Needie Seagoon': 0.7453022035139001, 'Eccles': 0.8116333563690437, 'Bluebottle': 0.7519653356706919, 'Henry Crun': 0.6944792261318251, 'Minnie Bannister': 0.6928871681167221, 'Hercules Grytpype-Thynne': 0.7154399707796903, 'Count Jim Moriarty': 0.6525961084709853, 'Major Dennis Bloodnok': 0.6236613189972134}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.