Python 元组列表中的最小值和最大值
我有一个元组列表,我试图获取每个索引的最小值和最大值。索引1似乎工作正常。然而,对于索引2,我期望值是(-2,11),但我得到的是(5,5)。谁能解释一下出了什么问题吗?多谢各位Python 元组列表中的最小值和最大值,python,list,tuples,max,min,Python,List,Tuples,Max,Min,我有一个元组列表,我试图获取每个索引的最小值和最大值。索引1似乎工作正常。然而,对于索引2,我期望值是(-2,11),但我得到的是(5,5)。谁能解释一下出了什么问题吗?多谢各位 test_list=[(-2,5),(-1,5),(0,6),(0,7),(0,7),(1,8),(2,9),(2,9),(3,10), (4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
test_list=[(-2,5),(-1,5),(0,6),(0,7),(0,7),(1,8),(2,9),(2,9),(3,10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
res1=最大值(测试列表)[0],最小值(测试列表)[0]
res2=最大值(测试列表)[1],最小值(测试列表)[1]
打印(“索引1的最小值:+str(res1))
打印(“索引2的最小值:+str(res2))
要获取每个元素的最小值和最大值,请使用以下命令:
要获取每个元素的最小值和最大值,请使用以下命令:
请尝试以下列表理解:
first_elements = [elem[0] for elem in test_list]
second_elements = [elem[1] for elem in test_list]
print(min(first_elements), max(second_elements))
print(max(first_elements), min(second_elements))
输出:
"-2 11"
"10 -1"
请尝试以下列表理解:
first_elements = [elem[0] for elem in test_list]
second_elements = [elem[1] for elem in test_list]
print(min(first_elements), max(second_elements))
print(max(first_elements), min(second_elements))
输出:
"-2 11"
"10 -1"
您可以简单地迭代列表,并使用
for
循环获取元组
t in的(测试列表):
打印(“最大:,最大(t),”\t最小:,最小(t))
结果:
Max: 5 Min: -2
Max: 5 Min: -1
Max: 6 Min: 0
Max: 7 Min: 0
Max: 7 Min: 0
Max: 8 Min: 1
...
The min of index 1 : -2
The min of index 2 : -1
The max of index 1 : 5
The max of index 2 : 5
您正在使用代码行读取max
和min
结果的索引:max(测试列表)[0],min(测试列表)[0]
尝试将索引[]
移动到测试列表的末尾,如下所示:max(测试列表[3])、min(测试列表[3])
您的代码应该如下所示:
test_list=[(-2,5),(-1,5),(0,6),(0,7),(0,7),(1,8),(2,9),(2,9),(3,10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
max1,min1=max(测试列表[0]),min(测试列表[0])
max2,min2=max(测试列表[1]),min(测试列表[1])
打印(“索引1的最小值:+str(最小值))
打印(“索引2的最小值:+str(最小值))
打印(“索引1的最大值:+str(max1))
打印(“索引2的最大值:+str(max2))
结果:
Max: 5 Min: -2
Max: 5 Min: -1
Max: 6 Min: 0
Max: 7 Min: 0
Max: 7 Min: 0
Max: 8 Min: 1
...
The min of index 1 : -2
The min of index 2 : -1
The max of index 1 : 5
The max of index 2 : 5
您可以简单地迭代列表,并使用for
循环获取元组
t in的(测试列表):
打印(“最大:,最大(t),”\t最小:,最小(t))
结果:
Max: 5 Min: -2
Max: 5 Min: -1
Max: 6 Min: 0
Max: 7 Min: 0
Max: 7 Min: 0
Max: 8 Min: 1
...
The min of index 1 : -2
The min of index 2 : -1
The max of index 1 : 5
The max of index 2 : 5
您正在使用代码行读取max
和min
结果的索引:max(测试列表)[0],min(测试列表)[0]
尝试将索引[]
移动到测试列表的末尾,如下所示:max(测试列表[3])、min(测试列表[3])
您的代码应该如下所示:
test_list=[(-2,5),(-1,5),(0,6),(0,7),(0,7),(1,8),(2,9),(2,9),(3,10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
max1,min1=max(测试列表[0]),min(测试列表[0])
max2,min2=max(测试列表[1]),min(测试列表[1])
打印(“索引1的最小值:+str(最小值))
打印(“索引2的最小值:+str(最小值))
打印(“索引1的最大值:+str(max1))
打印(“索引2的最大值:+str(max2))
结果:
Max: 5 Min: -2
Max: 5 Min: -1
Max: 6 Min: 0
Max: 7 Min: 0
Max: 7 Min: 0
Max: 8 Min: 1
...
The min of index 1 : -2
The min of index 2 : -1
The max of index 1 : 5
The max of index 2 : 5
考虑一下max(测试列表)[1]
。取所有元组的最大值,然后得到最大元组的第二个元素。我从这篇文章开始:我可能错了,但代码似乎在每个索引处返回max元素的元组。geeksforgeks是热垃圾。不惜一切代价避免它。我在那里看到的完全荒谬的建议数量令人震惊。它允许任何人,无论技能或知识如何,在不检查有效性或正确性的情况下发表文章。事实上,我曾试图更正他们的一些文章,但因“试图修改现有代码”而被拒绝;即使是现有的代码也完全没有意义。这就是元组比较在Python中的工作方式。然而,看起来你根本不是在寻找max/min元组,而是max/min第一和第二个元素。Python代码显然是由一个在Python方面经验很少的人编写的。他们似乎认为Python是C,所有内存都需要m