声明一个类People,成员变量有姓名,出生日期,性别,身高,体重等;生成10个类对象,并放在一个一维数组中,编写方法按身高进行排序
时间: 2024-03-26 15:35:21 浏览: 90
设计一个人类person,包括人员编号和姓名等数据成员以及相关的成员函数
好的,我会尽力回答您的问题。下面是代码示例:
```python
class People:
def __init__(self, name, birthday, gender, height, weight):
self.name = name
self.birthday = birthday
self.gender = gender
self.height = height
self.weight = weight
people_list = [People("张三", "1990-01-01", "男", 175, 65),
People("李四", "1992-02-02", "女", 170, 60),
People("王五", "1995-03-03", "男", 180, 70),
People("赵六", "1998-04-04", "女", 165, 50),
People("钱七", "2000-05-05", "男", 185, 75),
People("孙八", "2002-06-06", "女", 160, 45),
People("周九", "2005-07-07", "男", 190, 80),
People("吴十", "2008-08-08", "女", 155, 40),
People("郑十一", "2010-09-09", "男", 195, 85),
People("冯十二", "2012-10-10", "女", 150, 35)]
def sort_by_height(people_list):
return sorted(people_list, key=lambda x: x.height)
sorted_people_list = sort_by_height(people_list)
for p in sorted_people_list:
print(p.name, p.height)
```
以上代码中,我们定义了一个`People`类,包含了姓名、出生日期、性别、身高和体重等属性。我们创建了一个包含10个`People`对象的列表`people_list`,并使用`sorted()`函数按照身高进行排序,返回一个新的排序后的列表`sorted_people_list`。最后我们遍历打印排序后的列表中每个人的姓名和身高。
希望能对您有所帮助!
阅读全文