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

重庆建站程序家用电脑如何做网站服务器

重庆建站程序,家用电脑如何做网站服务器,wordpress网站整站搬迁,360排名检测在 Python 中#xff0c;列表和字典都是基础数据类型#xff0c;这两种数据类型会通过相互嵌套和多个层级形成复杂的数据类型#xff0c;类似 JSON 数据格式#xff0c;对列表和字典排序其实可以类比是对 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, reverseTrue) 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(reverseTrue)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(), reverseFalse) Out[4]: [Black, Blue, Green, Red, White, Yellow]对字典的键降序排序 In [5]: sorted(color, reverseTrue) Out[5]: [Yellow, White, Red, Green, Blue, Black]In [6]: sorted(color.keys(), reverseTrue) 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(), keylambda x: x[1]) Out[8]: [(White, 1),(Blue, 1),(Black, 2),(Green, 2),(Red, 3),(Yellow, 3)]字典排序完成后可以通过 dict() 函数将元组变回字典 In [15]: dict(sorted(color.items(), keylambda 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(), keylambda x: x[0], reverseTrue) 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(), keylambda x: x[0], reverseTrue)) 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(), keylambda 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(), keylambda 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(), keylambda 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(), keylambda x: x[1][0], reverseTrue) 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, reverseTrue) 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, keylambda 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, keylambda 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.zqtcl.cn/news/29689/

相关文章:

  • 软件工程做项目网站建设租房信息网站
  • 广东省住房和建设网站东莞有哪些大公司
  • 极限优化wordpress温州seo建站
  • 东莞免费做网站公司织梦网站栏目如何做下拉
  • 网站建设费用计算依据wordpress主题4mudi
  • 建网站去哪里备案电子商务网站建设与维护的主要内容
  • qq空间网站域名怎么做的佛山网页设计师培训
  • 西安那里做网站展示型网站一样做seo优化
  • 哪家公司网站做的好深圳龙华天气预报
  • 做学校的网站推广发展前景做自己的外贸网站怎样赚钱
  • 西安淘宝网站建设公司哪家好综合电子商务型企业网站
  • 做网站必要吗采购系统erp软件
  • 北京移动端网站公司网站关键词优化代码
  • 建设工程网站贴吧未来做那些网站致富
  • 中国空间站的意义科技类公司网站设计
  • 迷你世界怎么做网站期网站做竞价对seo有影响吗
  • 做淘客网站 知乎重庆 seo
  • 下载宝硬盘做网站新公司网站怎么做推广
  • 电商门户网站建设方案广东省备建设项目影响备案网站
  • 哪个网站可以做一对一老师德清网站建设
  • 哈尔滨住房和城乡建设局网站白云区做网站
  • 网站维护的方式包括域名备案是什么
  • 西安机械加工网站建设可以做网站挂在百度上吗
  • 权威迷失传奇新开网站手机适配网站
  • 兰溪市住房和城乡建设局网站wordpress注册跳转
  • 甘南网站建设公司优化seo搜索排名
  • 潍坊建设公司网站做网站有兼职吗
  • 无锡网站排名公司app下载应用
  • 网站建设 坚持实用原则拼团系统开发
  • 网站开发技术可以做什么工作国外做电商平台的网站有什么