Unable to understand TypeError
Can someone please explain why I'm getting this error? My code:
def x(n):
if n == 1: n = 4
elif n == 2: n = 3
elif n == 3: n = 2
elif n == 4: n = 1
return n
def y(n):
if n == 1: n = 2
elif n == 2: n = 1
elif n == 3: n = 4
elif n == 4: n = 3
return n
def query_x(i, j, quadrants):
quadrants[i-1:j] = map(x, quadrants[i-1:j])
def query_y(i, j, quadrants):
quadrants[i-1:j] = map(y, quadrants[i-1:j])
def query_c(i, j, quadrants):
count = [quadrants[i-1:j].count(n) for n in range(1,5)]
print "%d %d %d %d" % (count[0], count[1], count[2], count[3])
def process_queries(queries, quadrants):
for query in queries:
if query[0] == "X": query_x(query[1], query[2], quadrants)
elif query[0] == "Y": query_y(query[1], query[2], quadrants)
elif query[0] == "C": query_c(query[1], query[2], quadrants)
if __name__ == "__main__":
N = int(raw_input())
quadrants = []
for i in xrange(N):
pair = map(int, raw_input().split())
x, y = pair
if x > 0 and y > 0: quadrants.append(1)
elif x < 0 and y > 0: quadrants.append(2)
elif x < 0 and y < 0: quadrants.append(3)
elif x > 0 and y < 0: quadrants.append(4)
Q = int(raw_input())
queries = []
for i in xrange(Q):
query = raw_input().split()
queries.append([query[0], int(query[1]), int(query[2])])
process_queries(queries, quadrants)
Output:
4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
Y 1 2
C 1 3
1 1 1 1
Traceback (most recent call last):
File "QuadrantQueries_dev.py", line 53, in <module>
process_queries(queries, quadrants)
File "QuadrantQueries_dev.py", line 28, in process_queries开发者_JS百科
if query[0] == "X": query_x(query[1], query[2], quadrants)
File "QuadrantQueries_dev.py", line 16, in query_x
quadrants[i-1:j] = map(x, quadrants[i-1:j])
TypeError: 'int' object is not callable
pair = map(int, raw_input().split())
x, y = pair
On line 39 you are redefining the x and y objects to be integers. x and y no longer reference the functions that you defined at the top. Your "main" block of code is not a separate function scope you are still at the global namespace.
http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces
I think your code could use some improvements. I decided to rewrite the whole thing. Perhaps you will like some of my changes.
First, we can use dictionaries to handle the remapping of quadrant numbers. Second, we can make a helper function to compute the quadrant numbers. Then we can build the list of quadrant numbers and process it. Instead of reading queries, building a list of them, and then processing them later, this code just reads the queries and processes them.
dmapx = {1:4, 2:3, 3:2, 4:1}
dmapy = {1:2, 2:1, 3:4, 4:3}
def quadnum(x, y):
if x >= 0 and y >= 0:
return 1
elif x < 0 and y >= 0:
return 2
elif x < 0 and y < 0:
return 3
else:
return 4
def rewrite_x(quadrants, i, j):
quadrants[i-1:j] = [dmapx[x] for x in quadrants[i-1:j]]
def rewrite_y(quadrants, i, j):
quadrants[i-1:j] = [dmapy[y] for y in quadrants[i-1:j]]
def print_counts(quadrants, i, j):
count = [quadrants[i-1:j].count(n) for n in range(1,5)]
print "%d %d %d %d" % tuple(count)
def parse_quadrants():
n = int(raw_input())
for _ in xrange(n):
yield [int(s) for s in raw_input().split()]
def parse_queries():
n = int(raw_input())
for _ in xrange(n):
code, i, j = raw_input().split()
i = int(i)
j = int(j)
yield (code, i, j)
if __name__ == "__main__":
# parse quadrants from input, and build list of quadrant numbers
quadrants = [quadnum(x, y) for x, y in parse_quadrants()]
# parse and handle queries
for code, i, j in parse_queries():
if code == "X":
rewrite_x(quadrants, i, j)
elif code == "Y":
rewrite_y(quadrants, i, j)
elif code == "C":
print_counts(quadrants, i, j)
else
raise ValueError, "bad code: %s" % str(code)
精彩评论