Reputation: 121
x_list = [0, 0]
y_list = [0, 1]
def coordinates(condition_x, condition_y):
zipped = zip(x_list, y_list)
condition_coord = condition_x,condition_y
for a,b in zipped:
print a,b
coord = a,b
if condition_coord != coord:
x_list.append(condition_x)
y_list.append(condition_y)
else:
print "Already a room there"
print x_list
print y_list
coordinates(0,1)
print x_list
print y_list
For this code what I'm trying to accomplish is this: There are two lists x_list
and y_list
, and I'm checking to see if there are any coordinate pairs already there. The first index of x_list
and y_list
is the first coordinate, the second index of both of them is the second coordinate, etc.
In the coordinates
function I want to look at both of the lists, and test to see if that coordinate is already present, and if not it appends it to the 2 lists, creating another coordinate pair.
The error that I'm getting is when it's tested, it says both "Already a room there" and appends the coordinates to the list, even though they're already there.
Example:
coordindates(0,1)
coordindates(0,2)
is supposed to return "Already a room there" for the first call, and appends 0 to X_list and 2 to y_list for the second.
Upvotes: 1
Views: 95
Reputation: 22031
Your code shall be modified to update coordinates after performing for
loop over zipped x_list, y_list
, below is modified for loop with comments:
match = False
for a,b in zipped:
print a,b
coord = a,b
if condition_coord == coord: # coord matched
match = True # set match flag and break for loop, no reason to continue
break
if not match: # if match Falsy we have to update x_list and y_list
x_list.append(condition_x)
y_list.append(condition_y)
else:
print "Already a room there"
Or alternative version of coordinates
method:
def coordinates(cond_x, cond_y):
if (cond_x, cond_y) not in zip(x_list, y_list):
x_list.append(cond_x)
y_list.append(cond_y)
else:
print "Already there"
Upvotes: 0
Reputation: 78556
You should use a better test condition. As is, your condition adds a new coordinate when at least one test passes, instead, it should add when all the coordinates pass the test. You should use all
:
x_list = [0, 0]
y_list = [0, 1]
def coordinates(condition_x, condition_y):
zipped = zip(x_list, y_list)
condition_coord = condition_x,condition_y
#if all((a, b) != condition_coord for a, b in zipped):
if condition_coord not in zipped:
x_list.append(condition_x)
y_list.append(condition_y)
else:
print "Already a room there"
Upvotes: 0
Reputation: 15930
You're code can't work, since you do the check-and-add for every couple. So if you have N coordinates, you'll add the new coordinate N times, or N-1 if it's already present. You have to do something like this:
def coordinates(condition_x, condition_y):
zipped = zip(x_list, y_list)
condition_coord = condition_x,condition_y
in_lists = False
for a,b in zipped:
print a,b
coord = a,b
if condition_coord = coord:
in_lists = True
break
if not in_lists:
x_list.append(condition_x)
y_list.append(condition_y)
or better, just use a set
of tuples:
>>> coordinates = {(5, 7), (9, 0), (0, 1)}
>>> coordinates
set([(0, 1), (9, 0), (5, 7)])
>>> coordinates.add((5, 7))
>>> coordinates
set([(0, 1), (9, 0), (5, 7)])
>>> coordinates.add((4, 4))
>>> coordinates
set([(0, 1), (9, 0), (5, 7), (4, 4)])
Upvotes: 1
Reputation: 61
The problem is that you are just checking if one coordinate does not match your condition coord
. You could still search through but a better way is to harness Python's in
:
x_list = [0, 0]
y_list = [0, 1]
def coordinates(condition_x, condition_y):
if (condition_x, condition_y) not in zip(x_list, y_list):
x_list.append(condition_x)
y_list.append(condition_y)
else:
print 'Already a room there'
print x_list
print y_list
coordinates(0, 1)
print x_list
print y_list
Still better is to use a set
.
Upvotes: 1