Python 按列表列表的递增顺序对词典排序
我正试图按列表对词典进行排序。列表列表中的项目 字典里有钥匙 比如:Python 按列表列表的递增顺序对词典排序,python,list,dictionary,Python,List,Dictionary,我正试图按列表对词典进行排序。列表列表中的项目 字典里有钥匙 比如: 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'
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]
}
sort_by_increasing_order(mylist, myDict)
>> ['to','woodchuck','has','jam','who','farmer','black']
我要做的是
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
输出如下所示:
{'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]
}
sort_by_increasing_order(mylist, myDict)
>> ['to','woodchuck','has','jam','who','farmer','black']
当我尝试按列表排序时,注释行仅按字典键排序。
我的方法不正确。结果应该是一个长度递增的列表
如上文所述的指数。如果您的代码列表中出现语法错误,请尝试以下操作
def sort_by_increasing_order(mylist, myDict):
temp = sorted(myDict, key=lambda tempKey: [tempKey for tempKey in mylist], reverse=True )
return temp
在“代码列表理解语法错误”中,尝试以下操作:
def sort_by_increasing_order(mylist, myDict):
temp = sorted(myDict, key=lambda tempKey: [tempKey for tempKey in mylist], reverse=True )
return temp
在“代码列表理解语法错误”中,尝试以下操作:
def sort_by_increasing_order(mylist, myDict):
temp = sorted(myDict, key=lambda tempKey: [tempKey for tempKey in mylist], reverse=True )
return temp
在“代码列表理解语法错误”中,尝试以下操作:
def sort_by_increasing_order(mylist, myDict):
temp = sorted(myDict, key=lambda tempKey: [tempKey for tempKey in mylist], reverse=True )
return temp
看起来您正在尝试按照字典中列表的长度进行排序。这是您将如何做到的:
def sort_by_increasing_order(l, d):
return sorted(l, key=lambda i: len(d[i]))
不过,我建议您使用更清晰的命名方案。看起来您正试图按照字典中列表的长度进行排序。这是您将如何做到的:
def sort_by_increasing_order(l, d):
return sorted(l, key=lambda i: len(d[i]))
不过,我建议您使用更清晰的命名方案。看起来您正试图按照字典中列表的长度进行排序。这是您将如何做到的:
def sort_by_increasing_order(l, d):
return sorted(l, key=lambda i: len(d[i]))
不过,我建议您使用更清晰的命名方案。看起来您正试图按照字典中列表的长度进行排序。这是您将如何做到的:
def sort_by_increasing_order(l, d):
return sorted(l, key=lambda i: len(d[i]))
不过,我建议您使用更清晰的命名方案。我只是使用了您的输入列表来实现它
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']
我只是用你的输入列表来实现它
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']
我只是用你的输入列表来实现它
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']
我只是用你的输入列表来实现它
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']
什么的递增顺序?为什么
f
在您的示例中是第一个?我使用示例字典进行了更新。增加字典中索引长度的顺序等。这是否澄清了?@user2357112有什么建议吗?@user3247054您可以添加一些关于如何对列表排序的详细信息吗?可以解释第一个例子。@ashwini chaudhary用样本输出更新了。增加的顺序是什么?为什么f
在您的示例中是第一个?我使用示例字典进行了更新。增加字典中索引长度的顺序等。这是否澄清了?@user2357112有什么建议吗?@user3247054您可以添加一些关于如何对列表排序的详细信息吗?可以解释第一个例子。@ashwini chaudhary用样本输出更新了。增加的顺序是什么?为什么f
在您的示例中是第一个?我使用示例字典进行了更新。增加字典中索引长度的顺序等。这是否澄清了?@user2357112有什么建议吗?@user3247054您可以添加一些关于如何对列表排序的详细信息吗?可以解释第一个例子。@ashwini chaudhary用样本输出更新了。增加的顺序是什么?为什么f
在您的示例中是第一个?我使用示例字典进行了更新。增加字典中索引长度的顺序等。这是否澄清了?@user2357112有什么建议吗?@user3247054您可以添加一些关于如何对列表排序的详细信息吗?可以解释第一个示例。@ashwini chaudhary使用示例输出进行了更新。它修复了语法错误,但我没有得到最高阶的结果等。它修复了语法错误,但我没有得到最高阶的结果等。它修复了语法错误,但是我没有得到最高阶的结果等。它修复了语法错误,但我没有得到最高阶的结果等。谢谢你的回答,你的建议不起作用。我得到这个错误类型error:unhable类型:“list”。我有列表。@user3247054:您在示例中没有传递列表。@CHTONICDAEMON这只是为了传达我要达到的目的,等等,这就是为什么我给出了输入列表和字典的示例以澄清等等。谢谢您的回答,您的建议不起作用。我得到这个错误类型error:unhable类型:“list”。我有列表。@user3247054:您在示例中没有传递列表。@CHTONICDAEMON这只是为了传达我要达到的目的,等等,这就是为什么我给出了输入列表和字典的示例以澄清等等。谢谢您的回答,您的建议不起作用。我得到这个错误类型error:unhable类型:“list”。我有列表。@user3247054:您在示例中没有传递列表。@CHTONICDAEMON这只是为了传达我要达到的目的,等等,这就是为什么我给出了输入列表和字典的示例以澄清等等。谢谢您的回答,您的建议不起作用。我得到这个错误类型error:unhable类型:“list”。我有列表。@user3247054:您在示例中没有传递列表。@CHTONICDAEMON这只是为了传达我要实现的目标等,这就是为什么我给出了输入列表和字典示例以澄清等。谢谢您的回答。为什么是和,为什么是和。你能解释一下吗。我想黑色应该在那里。@user3247054每次有包含这个单词的列表时,我都会添加1
。或者我计算包含这个单词的列表,这就是sum(…)
所做的。。。(有用吗?更多解释?)谢谢你的回答。为什么是和,为什么是和。你能解释一下吗。我想黑色应该在那里。@user3247054每次有包含这个单词的列表时,我都会添加1
。或者我计算包含这个单词的列表,这就是sum(…)
所做的。。。(有用吗?更多解释?)谢谢你的回答。为什么是和,为什么是和。你能解释一下吗。我想黑色应该在那里。@user3247054每次有包含这个单词的列表时,我都会添加1
。或者我计算包含这个单词的列表,这就是sum(…)
所做的。。。(有用吗?更多解释?)谢谢你的回答。为什么是和,为什么是和。你能解释一下吗。我认为布莱克应该在那里@