def run(lst, tup):
tup_1 = ()
for x in range(len(lst)):
tup_1[x] = lst[x]
if tup_1[1] == lst[1]:
return True
for y in range(len(tup_1)):
if tup_1[y] == tup[y]:
return "matched"
else:
return "not equal"
print run([1,2,3],(1,2,3))
I tried to convert number in List to Tuple form, so I can compare with another tuple. But the problem is it returns an error like this:
Traceback (most recent call last): File "", line 16, in File "", line 5, in run TypeError: 'tuple' object does not support item assignment
You can convert a list to tuple with tuple
function like this
tuple(lst)
And to check if a list and tuple are the same, you can simply do this
return tuple(lst) == tup
This occurs because tuples are immutable, data inside them cannot be mutated
example:
>>> t = (1,2,3)
>>> t[1] = 4
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Unlike lists which are mutable:
>>> l = [1,2,3]
>>> l[1] = 4
>>> l
[1, 4, 3]
So use a list instead of a tuple:
tup_1 = []
If you need to convert a list to a tuple you can use the built-in tuple()
function
For more information I recommend you read the documentation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.