nums = [1]
target = 2
if target in nums:
print(nums.index(target))
else:
for i in range(len(nums)):
if target < nums[0]:
print(0)
break
elif nums[i] < target and nums[i+1] > target:
print(i+1)
elif target > nums[-1]:
print(len(nums))
break
nums = [1]
target = 2
for i in range(len(nums)):
if target < nums[0]:
print(0)
break
elif nums[i] == target:
print(i)
break
elif nums[i] < target and nums[i+1] > target:
print(i+1)
elif target > nums[-1]:
print(len(nums))
break
如果列表只有一个数字,这个方法会超出范围。