python递增列表_python – 按递增顺序按列表列表对字典进行排序

这篇博客探讨了如何按字典中列表的长度对字典进行递增排序。作者给出了一个函数`sort_by_increasing_order`的实现,但遇到了问题。解决方案是使用`lambda`表达式以列表长度为依据进行排序。
摘要由CSDN通过智能技术生成

我试图按列表列表对字典进行排序.列表列表中的项目

是字典中的键.

就像是:

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

当我尝试按列表排序时,注释行只是按字典键排序.

我的方法不正确.结果应该是具有增加的长度顺序的列表

如上所述的指数.任何想法

解决方法:

看起来您正在尝试按字典中列表的长度排序.这是你怎么做的:

def sort_by_increasing_order(l, d):

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

但是,我建议您使用更清晰的命名方案.

标签:python,list,dictionary

来源: https://codeday.me/bug/20191003/1847846.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值