I met a bug about numpy.
Here is my code:
huge_may_go = np.array(pd.DataFrame(columns=range(2)))
def add_may_go(x,y):
global huge_may_go
counter = 0
for i in range(-2,3):
print(huge_may_go)
cur_y = y i
if cur_y < 0 or cur_y >= board_order:
continue
for j in range(-2,3):
cur_x = x j
if (i == 0 and j == 0) or cur_x < 0 or cur_x >= board_order or ([cur_y,cur_x] == huge_may_go).all(1).any():
continue
if not public_grid[cur_y][cur_x]:
huge_may_go = np.append(huge_may_go,[[cur_y,cur_x]],axis=0)
counter = 1
return counter
I am not familiar with numpy. I used to use "list" of python to make it run.
However, the speed of list of Python is not fast.
One of my friends tell me to try numpy.
However i met a searious problem
Thanks to @Corralien. I know that my code is not wrong.
I finnal know which code bring my erro:
it is the line next to it:
huge_may_go = np.delete(huge_may_go,[[gy,gx]])
however, it lead to another question. Why is it wrong?
before the sentence the huge may go is:
[[7 7]
[7 8]
[7 9]
[7 10]
[7 11]
[8 7]
[8 8]
[8 9]
[8 10]
[8 11]
[9 7]
[9 10]
[9 11]
[10 7]
[10 8]
[10 9]
[10 10]
[10 11]
[11 7]
[11 8]
[11 9]
[11 10]
[11 11]]
however after it, it became:
[7 7 7 8 7 9 7 10 8 7 8 8 8 9 8 10 8 11 9 7 9 8 9 10 9 11 10 7 10 8 10 9 10 10 10 11 11 7 11 8 11 9 11 10 11 11]
I wanna know why
in this code, gx = 8 and gy = 9
Thanks to Corralien for another time
however, when i run it, the [9,8] is still in the array "huge_may_go"
or the way i delete is wrong?
thanks to everyone as well
I beg you to help me, Please.
In addition, if you have any idea of increase the speed of this code. Please tell me also.
The speed is the most important thing I care.
You may also tell me a way to impove the speed of the code instead. Thanks
I have search every where for this question. But my search engine tells me nothing!
In addition, if you have any idea of increase the speed of this code. Please tell me also.
The speed is the most important thing I care.
CodePudding user response:
Update
You have to reshape your array because numpy.delete
flatten it:
huge_may_go = np.delete(huge_may_go,[[9,8]]).reshape(-1, 2)
Your code seems to work fine for me:
huge_may_go = np.array(pd.DataFrame(columns=range(2)))
border_order = 19
public_grid = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] * 24
def add_may_go(x,y):
global huge_may_go
counter = 0
for i in range(-2,3):
print(huge_may_go)
cur_y = y i
if cur_y < 0 or cur_y >= board_order:
continue
for j in range(-2,3):
cur_x = x j
if (i == 0 and j == 0) or cur_x < 0 or cur_x >= board_order or ([cur_y,cur_x] == huge_may_go).all(1).any():
continue
if not public_grid[cur_y][cur_x]:
huge_may_go = np.append(huge_may_go,[[cur_y,cur_x]],axis=0)
counter = 1
return counter
add_may_go(8, 8)
Output:
[]
[[6 6]
[6 7]
[6 8]
[6 9]
[6 10]]
[[6 6]
[6 7]
[6 8]
[6 9]
[6 10]
[7 6]
[7 7]
[7 8]
[7 9]
[7 10]]
[[6 6]
[6 7]
[6 8]
[6 9]
[6 10]
[7 6]
[7 7]
[7 8]
[7 9]
[7 10]
[8 6]
[8 7]
[8 9]
[8 10]]
[[6 6]
[6 7]
[6 8]
[6 9]
[6 10]
[7 6]
[7 7]
[7 8]
[7 9]
[7 10]
[8 6]
[8 7]
[8 9]
[8 10]
[9 6]
[9 7]
[9 8]
[9 9]
[9 10]]
24
CodePudding user response:
Thanks everyone! I finnaly found out my mistakes and fixed it. especially thanks to corranli en.
By the way, if anyone have any idea to improve the speed of my code. i will be very appreciated
In addition, this is my another account. I can't login the account i post my question now. But i will reach it soon