python - NumPy 的新手,如何对 ndarray 进行排序(不仅仅是整数)?
问题描述
这是我第一次使用数组,一般来说我是 python 新手。我尽了最大努力,很好奇是否有人可以纠正我的错误并让我知道我哪里出错了!谢谢
# Names of a group of students
names = ['Alice', 'Edward', 'Timothy', 'James', 'Brandon', 'Mark']
# Age and graduation year of the above students
class_roster = [
[22, 2021],
[23, 2020],
[21, 2021],
[19, 2022],
[24, 2020],
[23, 2018]
]
我首先想到,我将如何一起检索姓名和班级名册?
我做了:名字+ class_roster
然后需要获取名册中最年长的人的索引并从名称列表中检索他们的姓名。
参考这个链接
我试过:出现“无法分配给操作员”错误
names + class_roster = ndarray
ndarray.ptp([axis, out])
在我得到上面输出最年长的人和他们的名字之后,我将如何在代码方面翻转它以显示最年轻的人的名字?
解决方案
分配工作相反:
ndarray = names + class_roster
另请注意,您目前没有任何 numpy 数组。对于您的问题:
names = ['Alice', 'Edward', 'Timothy', 'James', 'Brandon', 'Mark']
class_roster = [
[22, 2021],
[23, 2020],
[21, 2021],
[19, 2022],
[24, 2020],
[23, 2018]
]
# combine names with class_roster
people = [ x for x in zip(names,class_roster)]
# search for oldest student
oldest_person = max(people,key=lambda x:x[1][0])[0]
print("the oldest person is ",oldest_person)
# search for the youngest student
youngest_person = min(people,key=lambda x:x[1][0])[0]
print("the youngest person is ",youngest_person)
最后,创建一个以姓名和年龄为属性的类人可能会更好。或者,您可以使用 Pandas 库跟踪所有这些信息。
推荐阅读
- c++ - 为什么要调用 bool 类型转换?
- powershell - 需要在计划任务中运行两个操作。需要一个在第一次完成后运行
- python - Kivy - 定期更新标签文本
- css - 滚动到底部时,模态底部的填充会弄乱固定标题
- swift - 如何将 localhost 连接到 Swift API?
- java - sql2o - 选择查询从 VARCHAR 列中删除尾随空格
- python - 在电子邮件主题行中提取单词“for”和左括号“(”之间的单词。电子邮件主题行是输入
- javascript - 使用javascript重定向时如何找到重定向页面
- c - GtkApplication 的初始化——我应该使用 GObject 的“init”还是 GtkApplication 的“startup”?
- c# - 如何根据列表中的其他属性对列表进行排序