2017-05-05 38 views
-1

我有包含的值如下一个字典A中的一对表A的值列表的:排序其中containes对值基于列表B的值(键,值),其是

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

我转换成列表的

​​

我List_A现在是这样的:

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

新编辑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_B作为对我的List_A的价值。

现在让我们说我想根据我的List_B的end_year_score的数值对我的List_A进行排序。我会怎么做?

我已经试过这样:

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

'Michel PAULIN'从哪里来? c –

回答

2

你的元组有一个字符串和字典

这是你可以怎么做

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

谢谢你的工作:) – glaziko

+0

@glaziko随时准备提供帮助,请接受答案为正确答案 –