2015-04-19 44 views
0

所以我有一組名單,看起來像:減少所有值一個指定

[['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

回答

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 
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的列表,其中包含有意義的命名字段。

+0

好了,感謝您的反饋 – Hussain

+0

我沒有注意到每一個元素是一個字符串。我編輯我的解決方案。 – user162988

+0

你在變換函數裏面的「輸入」是什麼意思 - 當我調用函數時,我應該把列表的名字放在這裏嗎? – Hussain

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) 

請注意,我說,這是IT方面像你想要做的那樣。在你的位置,這是我會做的:

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)) 
+0

無論哪種方式,我都會得到相同的錯誤。它說,有太多的值需要解壓縮,因爲它的名字,課程,成績=學生[0] ...... – Hussain

+0

@Hussain期待3:抱歉。我錯過了那裏的一個微妙的錯誤。 – inspectorG4dget

+0

非常感謝,現在好像工作正常,我真的很感謝你的幫助 – Hussain