2017-05-05 3 views
-1

나는 다음과 같은 값이 포함 된 DICT A를이 목록 A의 한 쌍의 값 목록 B의 값을 기준으로 (키, 값)는 containes 목록 정렬 :쌍의 값

dict_A ={"George Washington":{ 
'lname':'Washinghton', 
'fname':'George', 
'class_id':'math', 
'teacher':'John Doe', 
'end_year_score' : 8} 

[(George Washington', {, u'fname': 'George', u'class_id' : 'math', u'teacher' : 'John Doe', u'end_year_score' : 8})] 

새로운 :

은 내가

List_A = [x for x in dict_A.iteritems()] 

내 List_A 이제 다음되는리스트로 변환 List_B

List_B = [(George Washington', {, u'fname': 'George', u'lname': 'Washington' u'class_id' : 'math', u'teacher' : 'John Doe', u'end_year_score' : 8}), (Genghis Khan', {u'fname': 'Genghis ', u'lname':'Khan', u'class_id' : 'math', u'teacher' : 'John Doe', u'end_year_score' : 6}), ('Queen Victoria', {u'fname': 'Queen', u'lname':'Victoria', u'class_id' : 'math', u'teacher' : 'John Doe', u'end_year_score' : 5}), etc...,] 

예를 편집하지만 내가 말했듯이 난 내 List_A 쌍의 값으로 List_B 있습니다.

이제 List_A를 end_year_score (내 List_B)의 숫자 값을 기준으로 정렬하고 싶다고합시다. 내가 어떻게 그럴 수 있니? 실제로 가능

List_A.sort(key=lambda x: x[4]) >>> IndexError: tuple index out of range 
List_A.sort(key=lambda x: x[1,4]) >>> TypeError: tuple indices must be integers, not tuple 
List_A.sort(key=lambda x: x[1]['end_year_score') >>> TypeError: unhashable type: 'dict' 

인가 :

내가이 봤어?

+0

죄송합니다 할 수있는 방법이다가, List_B은 무엇인가? 그러나 세 번째 구문은 정확하며 사용자가 제공하는 데이터에 적용됩니다. –

+1

'미쉘 폴린'은 어디에서 왔습니까? c –

답변

2

귀하의 튜플은 문자열과 딕셔너리

이 당신이 그것을

List_A.sort(cmp = lambda x,y:cmp(x[1]['end_year_score'],y[1]['end_year_score'])) 
+0

작동 해 주셔서 감사합니다. – glaziko

+0

@glaziko 언제든지 도와 드리겠습니다. 답변을 정답으로 사용하십시오. –