Python 使用字典索引并行数组?

Python 使用字典索引并行数组?,python,arrays,dictionary,Python,Arrays,Dictionary,我有4个并行数组,基于一个表示地图属性的表。每个数组大约有500个值,但所有数组都有相同数量的值 这些阵列是: start=流量累积较小的端点位置 end=另一个端点的位置(具有较大的流量累积) 长度=段长度,以及 shape=实际形状,定向为从开始到结束运行 我试图创建一个数据结构,从中可以使用递归函数来确定沿长度方向每2000m的起点和终点 以下问题和答案描述了我试图实现的目标: 如何将这4个并行数组存储在由start键控的字典中 我不熟悉编写函数、字典和在字典中使用数组。我正试图用Pyt

我有4个并行数组,基于一个表示地图属性的表。每个数组大约有500个值,但所有数组都有相同数量的值

这些阵列是:

start
=流量累积较小的端点位置

end
=另一个端点的位置(具有较大的流量累积)

长度
=段长度,以及

shape
=实际形状,定向为从开始到结束运行

我试图创建一个数据结构,从中可以使用递归函数来确定沿长度方向每2000m的起点和终点

以下问题和答案描述了我试图实现的目标:

如何将这4个并行数组存储在由start键控的字典中


我不熟悉编写函数、字典和在字典中使用数组。我正试图用Python完成这项任务。

我想这就是你的意思:

d = {}
for i in range(len(start)):
   d[start[i]] = (shape[i],length[i],end[i])

所以现在
d[一些开始值]
将保持相应的形状长度和结束值。

我想这就是你的意思:

d = {}
for i in range(len(start)):
   d[start[i]] = (shape[i],length[i],end[i])

因此,现在
d[一些开始值]
将保存相应的形状长度和结束值。

如果您想做一些类似Python的事情,可以使用
枚举

d = {}
for (i,st) in enumerate(start):
    d[st] = (shape[i],length[i],end[i])
或者更好-
zip

d = {}
for (st,sh,le,en) in zip(start,shape,length,end):
    d[st] = (sh,le,en)
请注意,您可以省略
for
循环的第一部分(即
for
关键字中的
之间)的旁白。我使用它们只是为了增强代码的可读性


与之相同,
d[some_start_value]
现在将保存相应的
形状
长度
结束
值。

如果您想做一些类似Python的事情,可以使用
枚举

d = {}
for (i,st) in enumerate(start):
    d[st] = (shape[i],length[i],end[i])
或者更好-
zip

d = {}
for (st,sh,le,en) in zip(start,shape,length,end):
    d[st] = (sh,le,en)
请注意,您可以省略
for
循环的第一部分(即
for
关键字中的
之间)的旁白。我使用它们只是为了增强代码的可读性


与之相同,
d[一些开始值]
现在将保留相应的
形状
长度
结束
值。

除了上述答案之外,我建议使用
命名双值
来简化访问:

from collections import namedtuple

# This creates a namedtuple called GISData. Name of the object and name in the first argument 
# should be the same. 
GISData = namedtuple('GISData', 'start shape length end')

# zip creates 1 list of 4-tuples from 4 single lists
# There are other ways to write this; this is just the shortest for me.
# Note that if you need this ordered, you should use an OrderedDict,
# which is in the collections module in python 2.7+, or you can find
# backported versions for python 2.6+. In those, the keys preserve ordering,
# so can still be searched as a list, which is useful if you need to find e.g.
# 479, which is not in the dictionary, but 400 and 500 are and you have to interpolate etc.

GISDict = dict((x[0], GISData(*x)) for x in zip(start, shape, length, end))

# The dictionary for any given start value
# Access the 4 individual pieces by name, or by index
GISDict[start_lookup].shape

等等。

除了上述答案之外,我建议使用
namedtuple
简化访问:

from collections import namedtuple

# This creates a namedtuple called GISData. Name of the object and name in the first argument 
# should be the same. 
GISData = namedtuple('GISData', 'start shape length end')

# zip creates 1 list of 4-tuples from 4 single lists
# There are other ways to write this; this is just the shortest for me.
# Note that if you need this ordered, you should use an OrderedDict,
# which is in the collections module in python 2.7+, or you can find
# backported versions for python 2.6+. In those, the keys preserve ordering,
# so can still be searched as a list, which is useful if you need to find e.g.
# 479, which is not in the dictionary, but 400 and 500 are and you have to interpolate etc.

GISDict = dict((x[0], GISData(*x)) for x in zip(start, shape, length, end))

# The dictionary for any given start value
# Access the 4 individual pieces by name, or by index
GISDict[start_lookup].shape

等等。

尽管此处不一定需要,但创建具有开始/结束/长度/形状属性的形状可能会很有用。这样,您就可以有一个
riversisegment
对象列表(或您想叫它们的任何对象),而不是四个属性列表。虽然这里不一定需要,但创建一个具有开始/结束/长度/形状属性的列表可能会很有用。这样,您就可以有一个
riversisegment
对象列表(或任何您想调用的对象),而不是四个属性列表。