我完全是python的新手,所以如果我甚至没有正确表达标题,我道歉…我有一些数据,如下所示:
array([[ 5, 9, 4, 11, -22.949, 0 ],
[ 5, 10, 3, 14, -22.454, 0 ],
........................
[ 4, 11, 4, 14, -21.952, 0 ]])其中前四列指的是两对地图数据。我还有一个地图键,我设法把它放到了字典里:
{'9': ['5', '9'],
'10': ['5', '10'],
..............
'64': ['4', '11']}基本上,我希望创建一个新的文本文件,其中的地图数据将替换为字典键,因此:
array([[5, 9, 4, 11, -22.949, 0]]看起来就像
array([[9, 64, -22.949, 0]]我只使用python进行绘图和一些计算(本质上是一个自动化的Excel……)因此,我将非常感谢在这个问题上的任何帮助。谢谢!
发布于 2017-01-28 00:16:18
import csv
import operator
coords = operator.itemgetter(0,1,2,3)
with open('path/to/data.csv') as infile, open('path/to/mapkey.csv') as mapfile:
ids = {}
for row in csv.reader(mapfile):
name, x, y = map(int, row)
ids[(x,y)] = name
answer = []
for x,y, a,b in map(coords, csv.reader(infile)):
answer.append([ids[(x,y)]], ids[(a,b)]])这将为您提供answer,这是一个2d列表,每个子列表中有两个映射键
https://stackoverflow.com/questions/41897747
复制相似问题