首页 > 编程语言 >Python 根据两个字段排序 中文排序 汉字排序 升序 降序

Python 根据两个字段排序 中文排序 汉字排序 升序 降序

时间:2022-10-30 11:03:54浏览次数:46  
标签:name iterator Python pinyin print stu Student 升序 排序

Python3写法

代码

# -*- coding: utf-8 -*-

# 需求:年龄倒序,姓名正序

from itertools import chain
from pypinyin import pinyin, Style


class Student:
def __init__(self, name, age):
self.name = name
self.age = age


def to_pinyin(stu):
lst = pinyin(stu.name, style=Style.TONE3) # 例:[['zhang1'], ['san1']]
print(lst)
iterator = chain.from_iterable(lst) # 迭代器
iterator_for_print = chain.from_iterable(lst) # 迭代器
print(iterator_for_print)
for item in iterator_for_print:
print(item)

# 写法一
return ''.join(iterator)

# 写法二
# return ''.join(chain.from_iterable(pinyin(stu.name, style=Style.TONE3)))


studentList = [
Student("张三", 25),
Student("小红", 22),
Student("王五", 25),
Student("小张", 22),
Student("李四", 25),
Student("小明", 22)
]

# 写法一
# studentList.sort(key=lambda stu: pinyin(stu.name, style=Style.TONE3))

# 写法二
studentList.sort(key=lambda stu: to_pinyin(stu))
studentList.sort(key=lambda stu: stu.age, reverse=True)

print("排序结果:")
for student in studentList:
print(str(student.age) + " " + student.name)

输出结果

Python 根据两个字段排序 中文排序 汉字排序 升序 降序_c#

Python2写法

代码

# -*- coding: utf-8 -*-

# 需求:年龄倒序,姓名正序

from itertools import chain
from pypinyin import pinyin, Style


class Student:
def __init__(self, name, age):
self.name = name
self.age = age


def to_pinyin(stu):
lst = pinyin(stu.name.decode("utf-8"), style=Style.TONE3) # 例:[['zhang1'], ['san1']]
print(lst)
iterator = chain.from_iterable(lst) # 迭代器
iterator_for_print = chain.from_iterable(lst) # 迭代器
print(iterator_for_print)
for item in iterator_for_print:
print(item)

# 写法一
return ''.join(iterator)

# 写法二
# return ''.join(chain.from_iterable(pinyin(stu.name.decode("utf-8"), style=Style.TONE3)))


studentList = [
Student("张三", 25),
Student("小红", 22),
Student("王五", 25),
Student("小张", 22),
Student("李四", 25),
Student("小明", 22)
]

# 写法一
# studentList.sort(key=lambda stu: pinyin(stu.name.decode("utf-8"), style=Style.TONE3))

# 写法二
studentList.sort(key=lambda stu: to_pinyin(stu))
studentList.sort(key=lambda stu: stu.age, reverse=True)

print("排序结果:")
for student in studentList:
print(str(student.age) + " " + student.name)

输出结果

Python 根据两个字段排序 中文排序 汉字排序 升序 降序_迭代器_02

C#的示例

代码

List<Student> list = new List<Student>()
{
new Student("张三", 25),
new Student("小红", 22),
new Student("王五", 25),
new Student("小张", 22),
new Student("李四", 25),
new Student("小明", 22)
};

//方法一,虽然写法繁琐,但思路清晰
list.Sort((a, b) =>
{
if (a.Age != b.Age)
{
return b.Age - a.Age;
}
else
{
return string.Compare(a.Name, b.Name);
}
});

//方法二,简捷清晰明了
//list = list.OrderByDescending(a => a.Age).ThenBy(a => a.Name).ToList();

foreach (var item in list)
{
Console.WriteLine(item.Age + " " + item.Name);
}

Console.Read();

class Student
{
public string Name { get; set; }

public int Age { get; set; }

public Student(string name, int age)
{
Name = name;
Age = age;
}
}

输出结果

Python 根据两个字段排序 中文排序 汉字排序 升序 降序_Python_03

对比C#,Python的坑

  1. Python默认的中文排序得不到预期的结果,需要引用pypinyin库解决,相当麻烦,要看懂这个代码,需要了解迭代器
  2. Python2的pypinyin库只支持unicode编码的字符串,必须通过decode转码,如果不转码,则抛出错误:must be unicode string or [unicode, ...] list
  3. Python没有大括号,无法直接在lambda表达式中写方法,方法必须定义在lambda表达式外部
  4. Python的lambda写法相对难以理解

经验丰富的Python程序员会说,这还不简单?
但是对于新手来说,非常不人性化,非常浪费时间。
我作为一个Python新手,就写个简单的排序程序,花了很长时间才学会怎么写,当然,确实没有去看文档,只通过百度以及在技术群里问,但是没有一个一口答出正确答案,最后自己摸索成功。
有人说Python2忘的差不多了,C#就不会忘。
用到pypinyin库时,还不习惯看pypinyin库的源码,pinyin方法的注释非常详细,不过没有C#这种强类型的语言看起来方便。
对比C#,Python的代码确实相对简捷。



标签:name,iterator,Python,pinyin,print,stu,Student,升序,排序
From: https://blog.51cto.com/u_5496753/5807422

相关文章

  • s004-桶排序
    s004-桶排序概念之前写的博客中讲述的排序算法,例如选择排序,冒泡排序,插入排序,快排,归并和堆排序都是基于比较的算法而桶排序不是基于比较的算法,而是基于数据状态的算法桶......
  • python 内部类调用相关
     内部类classMyOuter:age=18def__init__(self,name):self.name=nameclassMyInner:def__init__(self,inner_name):......
  • python精确计算浮点数
    因为二进制计算的问题,部分浮点数不能精确计算,如>>>1.1+2.23.30000000000003可以使用round(number,保留位数)或decimal包fromdecimalimportDecimala=Decima......
  • python导入包的注意事项
    模块导入使用import关键字。模块导入主要有以下形式importrequetsfromrequestsimportgetfromrequetsimport*创建的文件夹中只有包含__init.py__文件才......
  • python 封装 多态 继承 重写
    面向对象的三大特征封装:提高程序的安全性继承:提高代码的复用性(继承包括重写)多态:提高程序的可扩展性和可维护性 三大特征的实现:封装:将数据(属性)和......
  • Python 多重继承时metaclass conflict问题解决与原理探究
    背景最近有一个需求需要自定义一个多继承abc.ABC与django.contrib.admin.ModelAdmin两个父类的抽象子类,方便不同模块复用大部分代码,同时强制必须实现所有抽象方法,没想按想......
  • Python 实现栈的几种方式及其优劣
    1栈的概念栈由一系列对象对象组织的一个集合,这些对象的增加和删除操作都遵循一个“后进先出”(LastInFirstOut,LIFO)的原则。在任何时刻只能向栈中插入一个对象,但只能取得......
  • 编译安装python解释器
    Step1官网下载对应版本的压缩包如下图是python3.9.14版本的解释器Step2进入终端找到该压缩包并解压tarxfPython-3.9.14.tgzcdPython-3.9.14.tgzStep3编译安......
  • python3使用libpcap库进行抓包及数据处理
    python版本:python3.9libpcap版本:1.11.0b7pythonlibpcap库是底层绑定c语言libpcap库的开发包,旨在提供python应用可访问的unixclibpcap库API(以及为win32系统提供的Npca......
  • python 与C++ 利用socket实现json数据传输
    单机python与C++程序利用socket实现json数据传输目录单机python与C++程序利用socket实现json数据传输需求实现方法的选择具体实现流程图示涉及到的技术1socket......