Reputation: 13
I have a list in which 50 random numbers, ranging from 1 up to 1000, are sorted smallest to biggest. I want to find any number in between these numbers yet not including them. The problem is that when numbers follow each other directly (100, 101, 102) they are still getting printed even though the should not. (the code is written in Python 3.2.3)
I tried to take the first integer of the list an compare it to the second integer adding + 1 to the first one until it would eventually become as big as the second one. Then I would compare the second to the third one etc.
list = [98, 100, 101, 102, 103, 110, ... ]
position_counter_1 = 0
position_counter_2 = 1
position_1 = 0
position_2 = 1
while position_counter_2 < len(list):
if list[position_1] + 1 != list[position_2] :
while list[position_counter_1] <= list[position_counter_2] - 1 :
list[position_counter_1] = list[position_counter_1] + 1
print(list[position_counter_1])
if list[position_counter_1] >= list[position_counter_2] - 1 :
position_counter_1 = position_counter_1 + 1
position_counter_2 = position_counter_2 + 1
The output is 99 101 102 103 104 105 106 107 108 109
, it should be 99 104 105 106 107 108 109
.
Also I get the following error massage, which is not that bad since it only appears when the loop is done yet here it is:
while list[position_counter_1] <= list[position_counter_2] - 1 :
IndexError: list index out of range
Upvotes: 1
Views: 115
Reputation: 3862
I'm suggesting an easier approach without nested loops:
my_list = [98, 100, 101, 102, 103, 110, ... ]
for i in range(1000):
if i not in my_list:
print(i)
Upvotes: 0
Reputation: 7206
list = [98, 100, 101, 102, 103, 110 ]
for i in range (list[0], list[-1]):
if i not in list:
print (i)
output:
99
104
105
106
107
108
109
or list comprehension
print ([i for i in range (list[0], list[-1]) if i not in list])
Upvotes: 0
Reputation: 1048
You can use the range(start, stop, step) function:
start - position to start. Default is 0
stop - position to end, not included.
step - the incrementation. Default is 1
list = [98, 100, 101, 102, 103, 110]
for i in range(1,1001):
if i not in list:
print(i)
the output is:
... 99 104 105 106 107 108 109 ...
Upvotes: 0
Reputation: 3957
It's Python, don't complicate things. :)
mylist = [98, 100, 101, 102, 103, 110]
[num for num in range(mylist[0]+1, mylist[-1]) if num not in mylist]
Python 3 range
creates an iterator of all the integer numbers between the first argument (and includes it) and the second argument (not including it). So this list comprehension task from above creates a new list of all the numbers (mylist[0]
returns the first and mylist[-1]
returns the last integer), but doesn't include those already presented in the list (if num not in mylist
condition.)
Upvotes: 2
Reputation: 1598
You can use sets :)
list1 = [98, 100, 101, 102, 103, 110]
full = set(range(list1[0], list1[-1] + 1))
result = full - set(list1)
print(result)
# {99, 104, 105, 106, 107, 108, 109}
Here I'm creating a list with range
of all numbers between the range of your list, always assumming it is ordered, otherwise you can use sorted()
. Then casting it to set and just doing some difference of sets.
Upvotes: 0