2014-01-30 36 views
0

我想按列表列表排序字典。列表 列表中的项目是字典中的键。按升序排列列表中的字典

喜欢的东西:

sort_by_increasing_order(['d', 'e', 'f'], {'d': [0, 1], 'e': [1, 2, 3], 'f': [4]}) 
result: ['f', 'e', 'd'] 

我的输入列表是:

[ 
['why', 'was', 'cinderella', 'late', 'for', 'the', 'ball', 'she', 'forgot', 'to', 'swing', 'the', 'bat'], 
['why', 'is', 'the', 'little', 'duck', 'always', 'so', 'sad', 'because', 'he', 'always', 'sees', 'a', 'bill', 'in', 'front', 'of', 'his', 'face'], 
['what', 'has', 'four', 'legs', 'and', 'goes', 'booo', 'a', 'cow', 'with', 'a', 'cold'], 
['what', 'is', 'a', 'caterpillar', 'afraid', 'of', 'a', 'dogerpillar'], 
['what', 'did', 'the', 'crop', 'say', 'to', 'the', 'farmer', 'why', 'are', 'you', 'always', 'picking', 'on', 'me'] 
] 

我的字典里有些看起来是这样的:

{'to': [7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56], 
'jam': [20], 'black': [5], 'farmer': [11], 
'woodchuck': [54], 'has': [14, 16, 51], 'who': [16] 
} 

我有什么打算是

def sort_by_increasing_order(mylist, myDict): 
     #temp = sorted(myDict, key=myDict.get) 
     temp = sorted(myDict, key=lambda tempKey: for tempKey in mylist, reverse=True) 
     return temp 

输出将是这样的:

sort_by_increasing_order(mylist, myDict) 
    >> ['to','woodchuck','has','jam','who','farmer','black'] 

的注释行刚刚通过排序的字典键,当我试图通过列表进行排序。 我的方法不正确。结果应该是如上所述的具有指数长度 的递增顺序的列表。任何想法

+0

增加什么样的顺序?为什么在你的例子中'f'首先? – user2357112

+0

我更新了样本字典。字典中索引长度的增加顺序等等是否澄清? – user3247054

+0

@ user2357112有什么建议吗? – user3247054

回答

0

在代码列表中理解语法错误,试试这个,

def sort_by_increasing_order(mylist, myDict): 
     temp = sorted(myDict, key=lambda tempKey: [tempKey for tempKey in mylist], reverse=True) 
     return temp 
+0

它修复了语法错误,但我没有得到具有最高顺序的结果等。 – user3247054

2

它看起来像你正试图通过在字典中的列表的长度排序。这是你将如何做到这一点:

def sort_by_increasing_order(l, d): 
    return sorted(l, key=lambda i: len(d[i])) 

然而,我会建议你使用更清晰的命名方案。

+0

感谢您的回答,您的建议不起作用。我得到这个错误TypeError:不可用类型:'列表'。我有名单。 – user3247054

+0

@ user3247054:您没有传入示例中的列表列表。 – user2357112

+0

@chthonicdaemon这只是为了传达我想要实现的目标等,这就是为什么我给我的输入列表和字典的示例澄清等。 – user3247054

1

我只是用你的输入列表来实现它...

source = [ 
['why', 'was', 'cinderella', 'late', 'for', 'the', 'ball', 'she', 'forgot', 'to', 'swing', 'the', 'bat'], 
['why', 'is', 'the', 'little', 'duck', 'always', 'so', 'sad', 'because', 'he', 'always', 'sees', 'a', 'bill', 'in', 'front', 'of', 'his', 'face'], 
['what', 'has', 'four', 'legs', 'and', 'goes', 'booo', 'a', 'cow', 'with', 'a', 'cold'], 
['what', 'is', 'a', 'caterpillar', 'afraid', 'of', 'a', 'dogerpillar'], 
['what', 'did', 'the', 'crop', 'say', 'to', 'the', 'farmer', 'why', 'are', 'you', 'always', 'picking', 'on', 'me'] 
] 

# words you want to order in order of their number ov appearances 
words = ['to','woodchuck','has','jam','who','farmer'] 

sorted(words, key = lambda word: sum(1 for lst in source if word in lst), reverse=True) 

Out[38]: ['to', 'has', 'farmer', 'woodchuck', 'jam', 'who'] 
+0

感谢您的答案。为什么总结和为什么1总和。你能解释一下吗?我认为黑人应该在那里。 – user3247054

+0

@ user3247054每当有一个包含这个单词的列表,我就加上'1'。或者我计算包含这个词的列表,那是什么'sum(...)'做的...(推荐?更多解释?) – koffein