So I'm writing this code to see if an element in a list could be removed to make the list increasing. in the code below why do I get this error?
def almostIncreasingSequence(sequence):
sorted_sequence = sorted(sequence)
counter = 0
for i in len(sequence):
if sorted_sequence[i] != sequence[i]:
counter += 1
if counter > 1:
return True
else:
return False
len(sequence)
is a number here, and you can't iterate a number:
for i in len(sequence):
...
You probably wanted
for a,b in zip(sequence, sorted_sequence):
...
You may as well return the count rather than a boolean, or return from within the for-loop, since it's not necessary to iterate the entire sequence to check whether this count is > 1.
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.