I am writing a simple sort program in python and encounter this error. I want to swap list elements but it returns an error. I am attaching the error and program in question below.
我在python中编写一个简单的排序程序并遇到此错误。我想交换列表元素,但它返回错误。我正在附上下面的错误和程序。
list[i+1] = list[i]
TypeError: 'tuple' object does not support item assignment
Code:
码:
def my_sort(list):
for index in range(1,len(list)):
value=list[index]
i=index-1
while i>=0:
if value<list[i]:
list[i+1] = list[i]
list[i]=value
i=i-1
else:
break
return
input_list=eval(input("Enter list items"))
my_sort(input_list)
print(input_list)
1 个解决方案
#1
51
Evaluating "1,2,3" results in (1, 2, 3)
, a tuple
. As you've discovered, tuples are immutable. Convert to a list before processing.
评估“1,2,3”会产生(1,2,3)元组。正如您所发现的,元组是不可变的。在处理之前转换为列表。
#1
51
Evaluating "1,2,3" results in (1, 2, 3)
, a tuple
. As you've discovered, tuples are immutable. Convert to a list before processing.
评估“1,2,3”会产生(1,2,3)元组。正如您所发现的,元组是不可变的。在处理之前转换为列表。