用python输出列表第二个小的数
可以使用sorted()函数和索引来实现输出列表第二个小的数。
例如,假设列表为:
my_list = [5, 3, 1, 7, 2, 8]
则可以通过以下代码输出列表中第二个小的数:
sorted_list = sorted(my_list)
second_smallest = sorted_list[1]
print(second_smallest)
输出结果为:
2
解释:使用sorted()函数将列表排序后,第二个元素即为第二小的数。注意,索引从0开始,因此使用sorted_list[1]来访问第二个元素。
原文地址: http://www.cveoy.top/t/topic/bIb9 著作权归作者所有。请勿转载和采集!