TypeError: object of type 'types.GenericAlias' has no len()
Question:
I have one question, regarding why object of type "~~~" no len()
class testing:
def findrepeatnum(self, nums: list[int]) -> int:
i = 0
while i < len(nums):
if nums[i] == i:
i += 1
continue
if nums[nums[i]] == nums[i]:
return nums[i]
nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
return -1
if __name__ == '__main__':
num = list[2, 3, 4, 2, 5, 0, 1]
res = testing().findrepeatnum(num)
print(res)
This code gives me TypeError: object of type ‘types.GenericAlias’ has no len(). This confuses me for a long time. If I build the function like this:
def findrepeatnum(self, nums: list[int]) -> int:
i = 0
while i < len(nums):
if nums[i] == i:
i += 1
continue
if nums[nums[i]] == nums[i]:
return nums[i]
nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
return -1
if __name__ == '__main__':
num = list[2, 3, 4, 2, 5, 0, 1]
res = findrepeatnum(num)
print(res)
The code gives me TypeError: findrepeatnum() missing 1 required positional argument: ‘nums’ but the nums is already the parameter. I want to know how I can write this correctly.
Thank you very very much!
Answers:
The value of num should be:
num = [2, 3, 4, 2, 5, 0, 1]
# or
num = list([2, 3, 4, 2, 5, 0, 1])
list[int]
is used to specify that the values in the list are integer values.
so we cannot use list[] to define a list.
I have one question, regarding why object of type "~~~" no len()
class testing:
def findrepeatnum(self, nums: list[int]) -> int:
i = 0
while i < len(nums):
if nums[i] == i:
i += 1
continue
if nums[nums[i]] == nums[i]:
return nums[i]
nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
return -1
if __name__ == '__main__':
num = list[2, 3, 4, 2, 5, 0, 1]
res = testing().findrepeatnum(num)
print(res)
This code gives me TypeError: object of type ‘types.GenericAlias’ has no len(). This confuses me for a long time. If I build the function like this:
def findrepeatnum(self, nums: list[int]) -> int:
i = 0
while i < len(nums):
if nums[i] == i:
i += 1
continue
if nums[nums[i]] == nums[i]:
return nums[i]
nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
return -1
if __name__ == '__main__':
num = list[2, 3, 4, 2, 5, 0, 1]
res = findrepeatnum(num)
print(res)
The code gives me TypeError: findrepeatnum() missing 1 required positional argument: ‘nums’ but the nums is already the parameter. I want to know how I can write this correctly.
Thank you very very much!
The value of num should be:
num = [2, 3, 4, 2, 5, 0, 1]
# or
num = list([2, 3, 4, 2, 5, 0, 1])
list[int]
is used to specify that the values in the list are integer values.
so we cannot use list[] to define a list.