所以我有一个保存此变量的变量。
['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]
最好将这些子列表添加到数组列表中,例如说new_li = []并将列表附加到该列表中吗?
我想遍历每个姓名及其分数,以获得该学生的所有分数的平均数。为了更轻松地循环浏览,我将列表拼接成这样:
注意:Studentp_file是上述列表的列表变量。
for line in studentp_file:
answer = line[1:14]
结果是一个只有数字的新列表:
[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]
所以我想将第一行的值相加并得出平均值。然后对第二,第三等进行操作。
我将如何循环播放?我似乎无法使逐行循环顺利进行。
计划是稍后再添加名称。
谢谢。
答案 0 :(得分:0)
for sublist in list:
for value in sublist:
if not isinstance(value, float):
sublist.remove(value)
print(sublist)
我希望它能满足您的需求
答案 1 :(得分:0)
如果我们将所有lists
放在一个list
中,则可以遍历每个子列表并为每个切片的子列表mean
计算[1:]
,这将跳过第一项这是学生的姓名
列表理解
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]
扩展循环:
means = []
for i in l:
means.append(sum(i[1:])/float(len(i[1:])))
答案 2 :(得分:0)
您可以将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)}
收益:
{'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}