# -*- 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)
# -*- 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)
List list = new List()
{
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;
}
}
經(jīng)驗豐富的Python程序員會說,這還不簡單?
但是對于新手來說,非常不人性化,非常浪費時間。
我作為一個Python新手,就寫個簡單的排序程序,花了很長時間才學會怎么寫,當然,確實沒有去看文檔,只通過百度以及在技術群里問,但是沒有一個一口答出正確答案,最后自己摸索成功。
有人說Python2忘的差不多了,C#就不會忘。
用到pypinyin庫時,還不習慣看pypinyin庫的源碼,pinyin方法的注釋非常詳細,不過沒有C#這種強類型的語言看起來方便。
對比C#,Python的代碼確實相對簡捷。
成都創(chuàng)新互聯(lián)公司專注于龍游企業(yè)網(wǎng)站建設,自適應網(wǎng)站建設,商城網(wǎng)站定制開發(fā)。龍游網(wǎng)站建設公司,為龍游等地區(qū)提供建站服務。全流程按需網(wǎng)站制作,專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務