将所有值减少到指定的值

时间:2015-04-19 21:56:23

标签: sorting python-3.x

所以我有一组列表如下:

[['Amy,1,"10,10,6"'], ['Bella,3,"4,7,2"'], ['Cendrick,3,"5,1,9"'], ['Fella,2,"3,8,4"'], ['Hussain,1,"9,4,3"'], ['Jamie,2,"1,1,1"'], ['Jack,3,"10,8,0"'], ['Thomas,2,"5,0,5"'], ['Zyra,1,"7,8,7"']]

名称后面的数字是学生的班级编号,以下3个数字是该学生得分的3个分数。

我已经将它从一个有组织的列表组中分类到这个按字母顺序排列的列表但是我对以下内容有困难:

我希望能够按字母顺序对它们进行排序,但仅针对特定类别和最后三个值中的最高分数。例如,如果我想对第2类进行排序,那么输出将如下:

Fella,8
Jamie,1
Thomas,5

由于名称已按字母顺序排序,所有学生均来自2级。每个学生的高分也被放在他们旁边。

我真的很感激任何帮助。 TIA

3 个答案:

答案 0 :(得分:1)

也许这会奏效:

def transform(inputs, class_number):
  results = []
  for input in inputs:
    input = input[0]
    input_pieces = input.split(',', 2)
    if input_pieces[1] != class_number:
      continue
    scores = input_pieces[2].strip('"').split(',')
    results.append((input_pieces[0], max(scores)))
  return results

另外,我强烈建议您使用一些东西来为您的数据提供比逗号分隔的字符串更多的结构。像collections.namedtuple这样的东西。然后你可以得到一个带有有意义命名字段的namedtuple列表。

答案 1 :(得分:1)

您的数据结构已经过时了。看起来您希望在列表中保存有关每个学生的信息,但最终只在该列表中放入一个以逗号分隔的字符串。然后,您最终得到了一个列表列表,每个列表都包含一个这样的字符串。

这就是你想要做的事情:

[[Amy, 1, 10,10,6],
 [Bella, 3, 4,7,2],
 [Cendrick, 3, 5,1,9],
 [Fella, 2, 3,8,4],
 [Hussain, 1, 9,4,3],
 [Jamie, 2, 1,1,1],
 [Jack, 3, 10,8,0],
 [Thomas, 2, 5,0,5],
 [Zyra, 1, 7,8,7]
]

以下是您如何将所拥有的内容转换为您想要的内容:

students = []

for student in myList:  # myList is the list that you already have
    s = []
    name, course, grades = student[0].split(',', 2)
    s.append(name)
    s.append(int(course))
    s.extend([int(i) for i in grades.strip('"').split(',')])
    students.append(s)

完成此操作后,您可以按如下方式对students进行过滤和排序:

import operator
classNum = 1  # let's say you want all the students from class number 1
answer = sorted([s for s in students if s[1]==classNum], key=operator.itemgetter(0))

for student in answer:
    name = student[0]
    grade = max(student[2:]))
    print(name, grade)

请注意,我说这是您想要做的事情。在你的位置,这就是我要做的:

from collections import namedtuple as ntuple

Student = ntuple('Student', ['name', 'course', 'grades'])
students = []

courseNum = 1
for student in myList:  # myList is the list that you already have
    s = Student
    name, course, grades = student[0].split(',', 2)
    course = int(course)
    if course != courseNum: continue
    grades = [int(i) for i in grades.strip('"').split(',')]
    students.append(Student(name, course, grades))

students.sort(key=operator.attrgetter('name'))
for student in students:
    print(student.name, max(student.grades))

答案 2 :(得分:0)

以下是解决方案,以及您目前存储数据的方式 - 使处理变得困难。

>>> lst = [['Amy,1,"10,10,6"'], ['Bella,3,"4,7,2"'], ['Cendrick,3,"5,1,9"'],
 ['Fella,2,"3,8,4"'], ['Hussain,1,"9,4,3"'], ['Jamie,2,"1,1,1"'], ['Jack,3,"10,8,0"'], ['Thomas,2,"5,0,5"'], ['Zyra,1,"7,8,7"']]

>>> from itertools import chain
>>> lst_flat = chain.from_iterable(lst)
>>> sorted_lst = sorted(filter(lambda x: x.split(',')[1] == '2', lst_flat))
>>> print map(lambda x: (x.split(',')[0],
              max([int(y) for y in x.split('"')[1].split(',')])), sorted_lst)

[('Fella', 8), ('Jamie', 1), ('Thomas', 5)]

您应该考虑清理您表示数据的方式:

>>> from pprint import pprint
>>> from itertools import chain

>>> lst_clean = []
>>> for item in chain.from_iterable(lst):
    ... name, cls = item.split(',')[0], item.split(',')[1]
    ... marks = [int(x) for x in item.split('"')[1].split(',')]
    ... lst_clean.append((name, cls, marks))

>>> pprint(lst_clean)

[('Amy', '1', [10, 10, 6]),
('Bella', '3', [4, 7, 2]),
('Cendrick', '3', [5, 1, 9]),
('Fella', '2', [3, 8, 4]),
('Hussain', '1', [9, 4, 3]),
('Jamie', '2', [1, 1, 1]),
('Jack', '3', [10, 8, 0]),
('Thomas', '2', [5, 0, 5]),
('Zyra', '1', [7, 8, 7])]

>>> sorted_lst = sorted([(name, cls, marks) for (name, cls, marks) in lst_clean if cls == '2'])
>>> for name, cls, marks in sorted_lst:
    ... print name, max(marks)

Fella 8
Jamie 1
Thomas 5