从列表和循环中获取子列表

2024-06-11 12:01:24 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个变量来保存这个。你知道吗

['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\u 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]

所以我想把第一行的值加起来,得到平均值。然后第二次,第三次,依此类推。你知道吗

我怎么循环这个?我似乎不能让一行一行的循环顺利进行。你知道吗

计划是稍后再加上这些名字。你知道吗

谢谢。你知道吗


Tags: 列表line名字分数file平均值crunhenry
3条回答

您可以将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}

如果我们把所有的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:])))
for sublist in list:
    for value in sublist:
        if not isinstance(value, float):
            sublist.remove(value)
    print(sublist)

我希望它能满足你的要求

相关问题 更多 >