简体   繁体   English

列表中的最大值

[英]Max value from list

I have a list that consists of temperatures我有一个包含温度的列表

['24', '7', '5', '34', '41', '41', '4', '24', '45', '41']

when I attempt to get the max value by using当我尝试通过使用获得最大值时

maxTList = ['24', '7', '5', '34', '41', '41', '4', '24', '45', '41']
print(max(maxTList))

I got a return value of 7 .我得到了7的返回值。 What gives?是什么赋予了? Where am I going wrong?我哪里错了? I am expecting to return a value of 45 .我期望返回值45

What you have are strings, not numbers.你拥有的是字符串,而不是数字。 You can cast them to ints when computing the max or change your entire list to ints:您可以在计算最大值时将它们转换为整数或将整个列表更改为整数:

max(maxTList, key=int)

or要么

num_list = [int(x) for x in maxTList]
max(num_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM