如何显示最大值和最小值及其相关值
如何让它显示最大值和最小值及其相关值?我看到了 lambda 的答案,但我不明白。请帮忙。谢谢你!
with open('life-expectancy.csv', "r") as life_expectancy:
next(life_expectancy)
country = []
codes = []
years = []
expectancies = []
for data in life_expectancy:
clean_data = data.strip()
split_data = clean_data.split(',')
entity = split_data[0]
code = split_data[1]
year = (split_data[2])
expectancy = float(split_data[3])
country.append(split_data[0])
codes.append(split_data[1])
years.append(int(split_data[2]))
expectancies.append(float(split_data[3]))
这里的这部分显示个人的最大值和最小值,但它们彼此不相关——期望、实体和年份。
print(f'The overall max life expectancy is: {max(expectancies):.2f} from {max(entity)} in {max(years)}')
print(f'The overall min life expectancy is: {min(expectancies)} from {min(entity)} in {min(years)}')
How can I make it display the max and min with its relevant value? I saw an answer with lambda but I don't understand it. Please help. Thank you!
with open('life-expectancy.csv', "r") as life_expectancy:
next(life_expectancy)
country = []
codes = []
years = []
expectancies = []
for data in life_expectancy:
clean_data = data.strip()
split_data = clean_data.split(',')
entity = split_data[0]
code = split_data[1]
year = (split_data[2])
expectancy = float(split_data[3])
country.append(split_data[0])
codes.append(split_data[1])
years.append(int(split_data[2]))
expectancies.append(float(split_data[3]))
This part here display individual max and min but they are not related to each other--expectancies, entity, and year.
print(f'The overall max life expectancy is: {max(expectancies):.2f} from {max(entity)} in {max(years)}')
print(f'The overall min life expectancy is: {min(expectancies)} from {min(entity)} in {min(years)}')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,问题是当您调用
min(entity)
和max(entity)
时,Python 并不知道您正在谈论预期寿命。最小值或最大值基于entity
中字符串的字典顺序。min
和max
提供可选的key
参数,可让您定义用于确定最小或最大元素的顺序。举一个简单的例子,如果我们有一个字符串数组
strings
并且想要获取最长字符串,我们可以这样做:(是的,我知道我们可以这样做
key=len
这里但我试图让事情变得简单并与我的答案的其余部分保持一致。)这告诉Python我们希望最大基于每个字符串的长度。 lambda 告诉 Python 如何处理数组的每个元素以确定其顺序。
因为每列数据都有一个单独的数组,所以实体和预期寿命之间的唯一关联是它们的索引,即
entity[i]
的预期寿命是expectancies[i ]
。因此,我们需要找到具有最小和最大预期寿命的指数。我们可以通过以下方式做到这一点:
正如其他人所提到的,最好重新构建代码,以便将相关数据存储在一起,或者使用 Pandas 等库。
Indeed, the problem is when you call
min(entity)
andmax(entity)
, Python has no idea that you're talking about life expectancy. The minimum or maximum is based on the lexicographic ordering of the strings inentity
.min
andmax
offer an optionalkey
parameter which lets you define the ordering that should be used to determine the minimum or maximum element.As a simple example, if we had an array
strings
of strings and wanted to get the longest string, we could do:(Yes, I know we could do
key=len
here but I'm trying to keep things simple and consistent with the rest of my answer.)This tells Python that we want the maximum to based on the length of each string. The lambda tells Python what to do to each element of the array to determine its ordering.
Because you have a separate array for each column of your data, the only association we have between entities and life expectancies are their indices, i.e. the life expectancy of
entity[i]
isexpectancies[i]
. We will therefore need to find the index with the minimum and maximum life expectancy.We can do this by:
As others have alluded to, it may be best to restructure your code so that you're storing related data together, or use a library such as Pandas.
我建议创建一个类,在其中保存所有彼此相关的信息。那么您就只有该类的一个实例列表。当您找到最大期望值时,该对象还将携带所有其他相关数据。
另一种方法(也许更简单)是获取 max(expectancies) 的索引,然后在所有其他列表中查看该索引。但您必须确保所有列表包含相同数量的项目。
使用 numpy 时,您的打印代码可能如下所示:
I would suggest creating a class where you keep all the information related to each other. Then you have only one list of instances of this class. When you find the max expectancy, the object would carry all the other related data as well.
Another approach (perhaps easier) is to get index of the
max(expectancies)
and then just look at that index in all the other lists. But you have to make sure all the lists contain the same number of items.Your print code could look like this when using numpy: