Python 从列表和循环中获取子列表
我有一个变量,它保存着这个Python 从列表和循环中获取子列表,python,python-3.x,Python,Python 3.x,我有一个变量,它保存着这个 ['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.69473684210526
['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_文件是上述列表的列表变量
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]
所以我想把第一行的值加起来,得到平均值。然后对第二个、第三个,依此类推
我如何循环这个?我似乎无法使逐行循环顺利进行
计划是稍后再添加这些名称
谢谢
for sublist in list:
for value in sublist:
if not isinstance(value, float):
sublist.remove(value)
print(sublist)
我希望它能满足您的要求如果我们把所有的
列表
放在一个列表
中,然后我们可以循环遍历每个子列表,并计算每个切片子列表的平均值[1::][/code>,这将跳过第一项,即学生的姓名
列表理解
扩展循环:
means = []
for i in l:
means.append(sum(i[1:])/float(len(i[1:])))
您可以使用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}
您可以使用sum(answer)
print([sum(x)/len(x)表示lst中的x])添加这些数字。
给出平均值,其中lst
是您的列表。
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}