我有一个 csv,其中包含伦敦地铁站的名称和纬度/经度位置信息。它看起来像这样:
Station Lat Lng
Abbey Road 51.53195199 0.003737786
Abbey Wood 51.49078408 0.120286371
Acton 51.51688696 -0.267675543
Acton Central 51.50875781 -0.263415792
Acton Town 51.50307148 -0.280288296
我希望转换此 csv 以创建这些站的所有可能组合的原始目的地矩阵。有 270 个站点,因此有 72,900 种可能的组合。
最终我希望将此矩阵转换为具有以下格式的csv
O_Station O_lat O_lng D_Station D_lat D_lng
Abbey Road 51.53195199 0.003737786 Abbey Wood 51.49078408 0.120286371
Abbey Road 51.53195199 0.003737786 Acton 51.51688696 -0.267675543
Abbey Road 51.53195199 0.003737786 Acton Central 51.50875781 -0.263415792
Abbey Wood 51.49078408 0.120286371 Abbey Road 51.53195199 0.003737786
Abbey Wood 51.49078408 0.120286371 Acton 51.51688696 -0.267675543
Abbey Wood 51.49078408 0.120286371 Acton Central 51.50875781 -0.263415792
Acton 51.51688696 -0.267675543 Abbey Road 51.53195199 0.003737786
Acton 51.51688696 -0.267675543 Abbey Wood 51.49078408 0.120286371
Acton 51.51688696 -0.267675543 Acton Central 51.50875781 -0.263415792
第一步是将使用循环的任何站点与所有其他可能的站点配对。然后,我需要删除起点和终点是同一站的 0 个组合。
我试过使用 NumPy 函数 column_stack。然而,这给出了一个奇怪的结果。
import csv
import numpy
from pprint import pprint
numpy.set_printoptions(threshold='nan')
with open('./London stations.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
Stations = ['{O_Station}'.format(**row) for row in reader]
print(Stations)
O_D = numpy.column_stack(([Stations],[Stations]))
pprint(O_D)
输出
车站 =
['Abbey Road', 'Abbey Wood', 'Acton', 'Acton Central', 'Acton Town']
O_D =
array([['Abbey Road', 'Abbey Wood', 'Acton', 'Acton Central', 'Acton Town',
'Abbey Road', 'Abbey Wood', 'Acton', 'Acton Central', 'Acton Town']],
dtype='|S13')
理想情况下,我正在寻找更合适的功能,并且很难在 Numpy 手册中找到它。