最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python中根据某个字段排序数组

Python crifan 2191浏览 0评论
折腾:
【未解决】从匹配的170个host所找到的历史url手动整理出规则
期间,对于:
发现很多域名是被间隔开了。
然后去:
给数组排序,根据第一个字段,然后再去保存,就可以了。
 
python sort list by value
python – How to sort a list of objects based on an attribute of the objects? – Stack Overflow
python – How to sort a list of lists by a specific index of the inner list? – Stack Overflow
from operator import attrgetter
# from operator import itemgetter

    sortedUrlDictList = uniquedMatchedUrlDictList
    # sortedUrlDictList.sort(key = itemgetter(1))
    sortedUrlDictList.sort(key = attrgetter("hostRule"))
报错:
发生异常: AttributeError
'dict' object has no attribute 'hostRule'
python sort dict list
python – Sorting a dictionary with lists as values, according to an element from the list – Stack Overflow
python – How do I sort a list of dictionaries by a value of the dictionary? – Stack Overflow
实在不行就用lamda
newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 

from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name'))
最后是:
sortedUrlDictList.sort(key = itemgetter("hostRule"))
可以了。
其中每个dict的值是:
即可实现排序了:
【总结】
此处是dict的list:
[

{'hostRule': 'm.sanguosha.com', 'gameName': '三国杀', 'gameUrl': 'http://way.m.sanguos...ct=android'}

{'hostRule': '9game.cn', 'gameName': '仙魔变', 'gameUrl': 'http://sdklog.9game....os=android'}

{'hostRule': 'device.enjoymi.com', 'gameName': '三国战纪', 'gameUrl': 'http://newdevice.enj....com:8005/'}

。。。
]
此处去希望根据:hostRule的值去排序
代码:
from operator import itemgetter
sortedUrlDictList.sort(key = itemgetter("hostRule"))
即可。
排序后效果:

转载请注明:在路上 » 【已解决】Python中根据某个字段排序数组

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.195 seconds, using 23.31MB memory