当前位置: 首页 > news >正文

提高网站加载速度iis中国500强最新排名

提高网站加载速度iis,中国500强最新排名,高端网站建设哪家更专业,做网站用的编程工具在 Python 中,列表和字典都是基础数据类型,这两种数据类型会通过相互嵌套和多个层级形成复杂的数据类型,类似 JSON 数据格式,对列表和字典排序其实可以类比是对 JSON 排序。 列表排序 列表可以使用 sorted() 函数排序&#xff1…

Python 中,列表和字典都是基础数据类型,这两种数据类型会通过相互嵌套和多个层级形成复杂的数据类型,类似 JSON 数据格式,对列表和字典排序其实可以类比是对 JSON 排序。

列表排序

列表可以使用 sorted() 函数排序:

In [1]: color = ['White', 'Black', 'Red', 'Yellow', 'Green', 'Blue']In [2]: sorted(color)
Out[2]: ['Black', 'Blue', 'Green', 'Red', 'White', 'Yellow']

对列表降序排序:

In [3]: sorted(color, reverse=True)
Out[3]: ['Yellow', 'White', 'Red', 'Green', 'Blue', 'Black']

也可以使用列表内置的排序属性 list.sort()

In [1]: color = ['White', 'Black', 'Red', 'Yellow', 'Green', 'Blue']In [2]: color
Out[2]: ['White', 'Black', 'Red', 'Yellow', 'Green', 'Blue']In [3]: color.sort()In [4]: color
Out[4]: ['Black', 'Blue', 'Green', 'Red', 'White', 'Yellow']In [5]: color.sort(reverse=True)In [6]: color
Out[6]: ['Yellow', 'White', 'Red', 'Green', 'Blue', 'Black']

list.sort() 只有列表才有的属性,它会直接修改原列表并返回 None(原地排序)。而 sorted() 适用于任何可迭代的对象,如果你不需要原地排序使用 sorted() 会更加方便和高效。

字典排序

字典使用 sorted() 函数排序:

In [1]: color = {'White': 1, 'Black': 2, 'Red': 3, 'Yellow': 3, 'Green': 2, 'Blue': 1}In [2]: color
Out[2]: {'White': 1, 'Black': 2, 'Red': 3, 'Yellow': 3, 'Green': 2, 'Blue': 1}

对字典的键升序排序:

In [3]: sorted(color)
Out[3]: ['Black', 'Blue', 'Green', 'Red', 'White', 'Yellow']

sorted() 函数默认对字典的键升序排序,等同如下形式:

In [4]: sorted(color.keys(), reverse=False)
Out[4]: ['Black', 'Blue', 'Green', 'Red', 'White', 'Yellow']

对字典的键降序排序:

In [5]: sorted(color, reverse=True)
Out[5]: ['Yellow', 'White', 'Red', 'Green', 'Blue', 'Black']In [6]: sorted(color.keys(), reverse=True)
Out[6]: ['Yellow', 'White', 'Red', 'Green', 'Blue', 'Black']

对字典的值升序排序:

In [7]: sorted(color.values())
Out[7]: [1, 1, 2, 2, 3, 3]

这种排序结果是字典值的列表,所以一般情况下需要指定排序算法,通常使用 lambda 函数作为排序规则。

lambda x: x[1]x 是元组,x[0] 是键,x[1] 是值。

In [8]: sorted(color.items(), key=lambda x: x[1])
Out[8]: 
[('White', 1),('Blue', 1),('Black', 2),('Green', 2),('Red', 3),('Yellow', 3)]

字典排序完成后可以通过 dict() 函数将元组变回字典:

In [15]: dict(sorted(color.items(), key=lambda x: x[1]))
Out[15]: {'White': 1, 'Blue': 1, 'Black': 2, 'Green': 2, 'Red': 3, 'Yellow': 3}

在 Python 3.5(含)以前,字典是不能保证顺序的,键值对 A 先插入字典,键值对 B 后插入字典,但是当你打印字典的 Keys 列表时,你会发现 B 可能在A的前面。 但是从 Python 3.6 开始,字典是变成有顺序的了。

嵌套排序

上文只是对列表和字段排序进行单独的说明,但是在实际开发过程中嵌套排序才是经常遇到的,所以嵌套排序才是本文的重点。

通过排列组合可知嵌套排序有如下四种:

字典嵌套字典

In [1]: color = {...:     'White': {'level': 1},...:     'Black': {'level': 2},...:     'Red': {'level': 3},...:     'Yellow': {'level': 3},...:     'Green': {'level': 2},...:     'Blue': {'level': 1}...: }In [2]: color
Out[2]: 
{'White': {'level': 1},'Black': {'level': 2},'Red': {'level': 3},'Yellow': {'level': 3},'Green': {'level': 2},'Blue': {'level': 1}}

对字典的键升序排序:

In [3]: sorted(color.items())
Out[3]: 
[('Black', {'level': 2}),('Blue', {'level': 1}),('Green', {'level': 2}),('Red', {'level': 3}),('White', {'level': 1}),('Yellow', {'level': 3})]In [4]: dict(sorted(color.items()))
Out[4]: 
{'Black': {'level': 2},'Blue': {'level': 1},'Green': {'level': 2},'Red': {'level': 3},'White': {'level': 1},'Yellow': {'level': 3}}

对字典的键降序排序:

In [5]: sorted(color.items(), key=lambda x: x[0], reverse=True)
Out[5]: 
[('Yellow', {'level': 3}),('White', {'level': 1}),('Red', {'level': 3}),('Green', {'level': 2}),('Blue', {'level': 1}),('Black', {'level': 2})]In [6]: dict(sorted(color.items(), key=lambda x: x[0], reverse=True))
Out[6]: 
{'Yellow': {'level': 3},'White': {'level': 1},'Red': {'level': 3},'Green': {'level': 2},'Blue': {'level': 1},'Black': {'level': 2}}

字典嵌套列表

In [1]: color = {...:     'White': [250, 255, 251],...:     'Black': [0, 2, 1],...:     'Red': [255, 2, 0],...:     'Yellow': [255, 254, 0],...:     'Green': [1, 128, 0],...:     'Blue': [0, 1, 255]...: }In [2]: color
Out[2]: 
{'White': [250, 255, 251],'Black': [0, 2, 1],'Red': [255, 2, 0],'Yellow': [255, 254, 0],'Green': [1, 128, 0],'Blue': [0, 1, 255]}

对字典的键升序排序:

In [3]: sorted(color.items())
Out[3]: 
[('Black', [0, 2, 1]),('Blue', [0, 1, 255]),('Green', [1, 128, 0]),('Red', [255, 2, 0]),('White', [250, 255, 251]),('Yellow', [255, 254, 0])]In [4]: dict(sorted(color.items()))
Out[4]: 
{'Black': [0, 2, 1],'Blue': [0, 1, 255],'Green': [1, 128, 0],'Red': [255, 2, 0],'White': [250, 255, 251],'Yellow': [255, 254, 0]}

对字典中列表的值升序排序:

In [5]: sorted(color.items(), key=lambda x: x[1][0])
Out[5]: 
[('Black', [0, 2, 1]),('Blue', [0, 1, 255]),('Green', [1, 128, 0]),('White', [250, 255, 251]),('Red', [255, 2, 0]),('Yellow', [255, 254, 0])]In [6]: sorted(color.items(), key=lambda x: x[1][1])
Out[6]: 
[('Blue', [0, 1, 255]),('Black', [0, 2, 1]),('Red', [255, 2, 0]),('Green', [1, 128, 0]),('Yellow', [255, 254, 0]),('White', [250, 255, 251])]In [7]: sorted(color.items(), key=lambda x: x[1][2])
Out[7]: 
[('Red', [255, 2, 0]),('Yellow', [255, 254, 0]),('Green', [1, 128, 0]),('Black', [0, 2, 1]),('White', [250, 255, 251]),('Blue', [0, 1, 255])]

lambda x: x[1][0] 中,x[1][0] 代表按列表第一个值排序,以此类推。

对字典中列表的值降序排序:

In [8]: sorted(color.items(), key=lambda x: x[1][0], reverse=True)
Out[8]: 
[('Red', [255, 2, 0]),('Yellow', [255, 254, 0]),('White', [250, 255, 251]),('Green', [1, 128, 0]),('Black', [0, 2, 1]),('Blue', [0, 1, 255])]

列表嵌套列表

In [1]: color = [['White', 2], ['Black', 3], ['Red', 4],['White', 1], ['Black', 2], ['Red', 3]]In [2]: color
Out[2]: 
[['White', 2],['Black', 3],['Red', 4],['White', 1],['Black', 2],['Red', 3]]In [3]: sorted(color)
Out[3]: 
[['Black', 2],['Black', 3],['Red', 3],['Red', 4],['White', 1],['White', 2]]In [4]: sorted(color, reverse=True)
Out[4]: 
[['White', 2],['White', 1],['Red', 4],['Red', 3],['Black', 3],['Black', 2]]

列表嵌套字典

In [1]: colors = [...:     {'color': 'White', 'level': 2},...:     {'color': 'Black', 'level': 3},...:     {'color': 'Red', 'level': 4},...:     {'color': 'White', 'level': 1},...:     {'color': 'Black', 'level': 2},...:     {'color': 'Red', 'level': 3}...: ]In [2]: colors
Out[2]: 
[{'color': 'White', 'level': 2},{'color': 'Black', 'level': 3},{'color': 'Red', 'level': 4},{'color': 'White', 'level': 1},{'color': 'Black', 'level': 2},{'color': 'Red', 'level': 3}]

对列表中每个字典的 color 字段进行排序(单级排序):

In [3]: sorted(colors, key=lambda x: x['color'])
Out[3]: 
[{'color': 'Black', 'level': 3},{'color': 'Black', 'level': 2},{'color': 'Red', 'level': 4},{'color': 'Red', 'level': 3},{'color': 'White', 'level': 2},{'color': 'White', 'level': 1}]

对列表中每个字典的 color 字段进行排序后再对 level 字段排序(多级排序):

In [4]: sorted(colors, key=lambda x: (x['color'], x['level']))
Out[4]: 
[{'color': 'Black', 'level': 2},{'color': 'Black', 'level': 3},{'color': 'Red', 'level': 3},{'color': 'Red', 'level': 4},{'color': 'White', 'level': 1},{'color': 'White', 'level': 2}]

参考文章:
https://docs.python.org/zh-cn/3/howto/sorting.html
https://www.kingname.info/2019/07/13/python-dict
https://blog.csdn.net/ray_up/article/details/42084863

http://www.khdw.cn/news/3169.html

相关文章:

  • 自建购物网站多少钱网页设计模板网站免费
  • 益阳网站开发广东短视频seo营销
  • 哪里可以接做ppt的网站怎么做一个网站页面
  • 学校做网站及费用营销型网站名词解释
  • 阿里云如何添加新网站windows 优化大师
  • 那里可以做网站安卓aso优化工具
  • 51zwd一起做网站九江seo优化
  • 百合视频做爰视频网站上海搜索推广
  • 保定专业网站制作seo做得比较好的企业案例
  • 专业营销网站建设关键词seo优化公司
  • 小网站关键词搜什么百度应用市场下载安装
  • 网站开发设计培训怎么seo快速排名
  • 织梦做的网站总是被攻击搜索引擎营销的实现方法
  • 网站建设工资 优帮云整站优化是什么意思
  • 黄村做网站建设成品短视频app下载有哪些软件
  • WordPress nasseo全站优化全案例
  • 南宁市住房建设局网站关键词推广优化排名如何
  • 什么样的网站利于优化网站推广优化的原因
  • 做网站成本重庆网站关键词排名优化
  • 培训机构不退费最有效方式西藏自治区seo 标题 关键词优化
  • 什么网站可以做宣传单扬州百度关键词优化
  • 外贸网站搭建难不难淘宝店铺推广方式有哪些
  • 如何注册一个自己的网址无锡网站seo顾问
  • 青岛关键词快速排名免费seo网站
  • 吉林科技网站建设文山seo公司
  • 合肥网站建设bai nuo百度一下 你就知道首页官网
  • 漯河网站建设nba排名最新排名
  • 做网站卖袜子效益如何求好用的seo软件
  • 个人网站一键生成无安全警告的浏览器
  • flash网站优化金华seo