Python list.sort()未按第二个元组参数正确排序值
我试图通过Python3.5.2中元组中的第二个参数对元组列表进行排序,以找出哪些算法以升序的方式占用Python list.sort()未按第二个元组参数正确排序值,python,python-3.x,sorting,tuples,Python,Python 3.x,Sorting,Tuples,我试图通过Python3.5.2中元组中的第二个参数对元组列表进行排序,以找出哪些算法以升序的方式占用最少->最多的时间,但是出于某种原因,看起来是随机排序的。我的代码: import math def speeds(n): new_dictionary = {} six_n_log_n = 6 * n * math.log(n, 2) thr_n_exp05 = 3 * (n ** 0.5) four_expn = 4 ** n ceil_sqrt
最少->最多的时间,但是出于某种原因,看起来是随机排序的。我的代码:
import math
def speeds(n):
new_dictionary = {}
six_n_log_n = 6 * n * math.log(n, 2)
thr_n_exp05 = 3 * (n ** 0.5)
four_expn = 4 ** n
ceil_sqrt_n = math.ceil(math.sqrt(n))
five_n = 5 * n
n_cubed = n ** 3
log_log_n = math.log(math.log(n, 2))
n_exp_01 = n ** 0.01
floor_2_n_log_exp2_n = math.floor(2 * n * (math.log(n, 2)**2))
n_exp2_log_n = (n ** 2) * math.log(n, 2)
log_exp2_n = math.log(n, 2) ** 2
one_div_n = 1 / n
two_exp_n = 2 ** n
four_exp_logn = 4 ** (math.log(n, 2))
two_exp_logn = 2 ** (math.log(n, 2))
four_n_exp_threehalves = 4 * (n ** (3/2))
n_exp2 = n ** 2
sqrt_log_n = math.sqrt(math.log(n, 2))
new_dictionary[0] = six_n_log_n
new_dictionary[1] = thr_n_exp05
new_dictionary[2] = four_expn
new_dictionary[3] = ceil_sqrt_n
new_dictionary[4] = five_n
new_dictionary[5] = n_cubed
new_dictionary[6] = log_log_n
new_dictionary[7] = n_exp_01
new_dictionary[8] = floor_2_n_log_exp2_n
new_dictionary[9] = n_exp2_log_n
new_dictionary[10] = log_exp2_n
new_dictionary[11] = one_div_n
new_dictionary[12] = two_exp_n
new_dictionary[13] = four_exp_logn
new_dictionary[14] = two_exp_logn
new_dictionary[15] = four_n_exp_threehalves
new_dictionary[16] = n_exp2
new_dictionary[17] = sqrt_log_n
sorted_list = []
for key in new_dictionary:
sorted_list.append((key, new_dictionary[key]))
sorted_list.sort(key=lambda x: x[1])
for i, x in sorted_list:
print(sorted_list[i])
return sorted_list
n = 15
speeds(n)
预期的输出应该是按第二个参数升序排列的元组,但我收到的是:
(15, 232.379000772445)
(10, 15.263794126054286)
(14, 15.000000000000002)
(2, 1073741824)
(17, 1.9765855902562173)
(7, 1.027450511266727)
(9, 879.0503840119167)
(13, 225.00000000000006)
(3, 4)
(12, 32768)
(8, 457)
(5, 3375)
(11, 0.06666666666666667)
(4, 75)
(16, 225)
(1, 11.618950038622252)
(0, 351.6201536047667)
(6, 1.3627418135330593)
有谁能告诉我,为什么我会从中得到一个看似随机的顺序?似乎找不到我的问题所在。如果在排序后检查排序列表
,您将看到它已正确排序
[(11, 0), (7, 1.027450511266727), (6, 1.3627418135330593), (17, 1.9765855902562173), (3, 4.0), (1, 11.618950038622252), (14, 15.000000000000002), (10, 15.263794126054286), (15, 60), (4, 75), (16, 225), (13, 225.00000000000006), (0, 351.6201536047667), (8, 457.0), (9, 879.0503840119167), (5, 3375), (12, 32768), (2, 1073741824)]
错误发生在以下行中:
for i, x in sorted_list:
您没有像您所想的那样对键和值进行迭代。相反,这是将列表中的每个元组解包,并将其第一个组件分配给i
,将其第二个组件分配给x
。然后,您将在列表中的i
th位置访问元素,这将导致出现一个随机排序。你可以改为写:
for i, x in enumerate(sorted_list):
或者更简单地说,您可以打印要显示的元组
for item in sorted_list:
print(item)
如果在排序后检查sorted_list
,您将看到它已正确排序
[(11, 0), (7, 1.027450511266727), (6, 1.3627418135330593), (17, 1.9765855902562173), (3, 4.0), (1, 11.618950038622252), (14, 15.000000000000002), (10, 15.263794126054286), (15, 60), (4, 75), (16, 225), (13, 225.00000000000006), (0, 351.6201536047667), (8, 457.0), (9, 879.0503840119167), (5, 3375), (12, 32768), (2, 1073741824)]
错误发生在以下行中:
for i, x in sorted_list:
您没有像您所想的那样对键和值进行迭代。相反,这是将列表中的每个元组解包,并将其第一个组件分配给i
,将其第二个组件分配给x
。然后,您将在列表中的i
th位置访问元素,这将导致出现一个随机排序。你可以改为写:
for i, x in enumerate(sorted_list):
或者更简单地说,您可以打印要显示的元组
for item in sorted_list:
print(item)
迭代元组时,要打印元组本身:
否则,将根据索引的第一个值打印索引处的值。例如,排序列表中的第一个值是:
(11, 0)
实际上是在寻找:
sorted_list[11]
这就是为什么您会看到不正确的第一个值。当您迭代元组时,您希望打印元组本身:
否则,将根据索引的第一个值打印索引处的值。例如,排序列表中的第一个值是:
(11, 0)
实际上是在寻找:
sorted_list[11]
这就是为什么您会看到不正确的第一个值