设为首页 加入收藏

TOP

Python 根据两个字段排序 中文排序 汉字排序 升序 降序(一)
2023-07-23 13:45:47 】 浏览:58
Tags:Python 段排序 文排序 升序 降序

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)

输出结果

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)

输出结果

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;
    }
}

输出结果

对比C#,Python的坑

  1. Python默认的中文排序得不到预期的结果,需要引用pypinyin库解决,相当麻烦,要看懂这个代码,需要了解迭代器
  2. Python2的pypinyin库只支持unicode编码的字符串,必须通过decode转码,如果不转码,则抛出错误:must be unicode string or [unicode, ...] list
  3. Python没有大括号,无法直接在lambda表达式中写方法,方法必须
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇rest_framework认证源码分析 下一篇Python学习:构造函数与析构函数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目