我在 Python“lines_origin”和“lines_dest”中有两个元组以一种可能非常低效的方式生成!但是它们显示了连接:lines_origin[1] 连接到lines_dest[2] 等等。
我需要做的是,分别遍历所有可能坐标的单独“主”列表,返回其连接的点,然后根据另一个状态列表(开/关)检查该连接子列表。到目前为止,这是我的尝试:
import random
import numpy as np
### my list of all points
coordinates = []
for x in range(10):
for y in range(20):
coordinates.append((x,y))
### the connections between points
lines = []
while len(lines) < (800): # limiting each point to an average of 4 connections
rnd_pair = random.sample(coordinates, 2)
rnd_pair_rev = [rnd_pair[1],rnd_pair[0]]
rnd_pair_x = [x for x,y in rnd_pair]
rnd_pair_y = [y for x,y in rnd_pair]
distance = abs(rnd_pair_x[0] - rnd_pair_x[1]) + abs(rnd_pair_y[0] - rnd_pair_y[1])
if distance <=2:
if rnd_pair not in lines and rnd_pair_rev not in lines:
lines.append(rnd_pair)
else:
rnd_pair = []
rnd_pair_rev = []
rnd_pair_x = []
rnd_pair_y = []
### splitting the connections into two tuples
lines_origin, lines_dest = zip(*lines)
### an array of binary statuses, these line up to coordinates by index (so if
# states[6] = "ON" then this denotes that coordinates[6] is on.
states = np.random.choice(["ON", "OFF"], size = len(coordinates), replace=True, p=[0.2, 0.8])
### my failed attempt to iterate over the coordinates
for i in coordinates:
if coordinates[i] in lines_origin:
connections = lines_dest # this should return the coordinates of the points that coordinates[i]
# is connected to
if coordinates[i] in lines_dest:
connections.append(lines_origin) # as above but checking the destination points
list(set(connections)) # dedupe the connections
### check whether a point is connected to an "ON" point
for point in connections:
return states[coordinates.index(connections[point])]