Home>

### python 3x - i want to know the solution because it is not calculated in the loop

I would like to prepare a list of 3 randomly generated hub stations and 25 randomly generated stations, and then generate a list excluding the stations included in a certain radius circle of 3 hub stations. Is calculated only for the last element.

I know that I'm a beginner and stumbling in a rudimentary place, but I'd appreciate it if someone could tell me.
Thank you.

``````def remove_station (station_list, hub_list, R_big):
X, Y = hub_list [0], hub_list [1]
x, y = station_list [0], station_list [1]
hub_size = len (hub_list [0])
station_size = len (station_list [0])
for i in range (hub_size):
for j in range (station_size):
Dx = X [i]-x [j]
Dy = Y [i]-y [j]
b = math.sqrt (Dx * Dx + Dy * Dy)
m = []
n = []
if b-R_big>= 0: # If the distance between the station and the base station is more than R_big, it will be adopted as a station
m.append (x [j])
n.append (y [j])
else:
pass
return [m, n]``````
• Answer # 1

Hello.

The list m, n has been initialized after exiting the double loop, so only the last entry can enter m, n.
It is before the double loop that m and n are initialized.

Need to determine if the distance between a station and all hub stations is more than R_big
Because there is a double loop, the list of normal stations is outside and the list of hub stations is inside.

If you use random data without knowing whether the program is written correctly,
It becomes difficult to verify the program.

I corrected it based on that point. Please see below.

The coordinates of the hub station are as follows.
(25,25)
(75,25)
(75,75)

The coordinates of a normal station are as follows.
(25,20)
(75,30)
(75,60)
(25,75)
(25,60)

The radius R_big is 25.

As you can see by writing a picture, the two stations (25,75) and (25,60) are far from any hub station.

``````import math
def remove_station (station_list, hub_list, R_big):
X, Y = hub_list [0], hub_list [1]
x, y = station_list [0], station_list [1]
hub_size = len (hub_list [0])
station_size = len (station_list [0])
m = []
n = []
for j in range (station_size):
flag = True
for i in range (hub_size):
Dx = X [i]-x [j]
Dy = Y [i]-y [j]
b = math.sqrt (Dx * Dx + Dy * Dy)
if b<= R_big:
# If the distance between a hub station and the station is within R_big,
# Since it is not adopted as a station, set False to flag
flag = False
if flag == True:
# A station that is more than R_big away from any hub station
# Add to list m, n
m.append (x [j])
n.append (y [j])
return [m, n]
(station_list_x, station_list_y) = remove_station ([[25,75,75,25,25], [20,30,60,75,60]], [[25,75,75], [25,25,75] ],twenty five)
print ('x =')
print (station_list_x)
print ('y =')
print (station_list_y)``````

The execution result is as follows.

``````x =
[25, 25]
y =
[75, 60]``````

I'm glad if you can use it as a reference.

• Answer # 2

Impressions I saw briefly.

I don't think it will work because there is no if statement in the for statement.