Python:元组中的列表

Python:元组中的列表,python,list,python-2.7,tuples,Python,List,Python 2.7,Tuples,我有以下格式的开放式街道地图数据: </way> <way id="148531879"> <nd ref="1616241466"/> <nd ref="1616241469"/> <nd ref="1616241471"/> <nd ref="1616241472"/> <nd ref="1616241475"/> <nd ref="1616241479"

我有以下格式的开放式街道地图数据:

</way>
<way id="148531879">
    <nd ref="1616241466"/>
    <nd ref="1616241469"/>
    <nd ref="1616241471"/>
    <nd ref="1616241472"/>
    <nd ref="1616241475"/>
    <nd ref="1616241479"/>
    <nd ref="276928691"/>
    <tag k="highway" v="secondary"/>
    <tag k="lit" v="no"/>
    <tag k="oneway" v="yes"/>
    <tag k="ref" v="L 292"/>
</way>
<way id="10870759">
    <nd ref="96594201"/>
    <nd ref="96594205"/>
    <nd ref="96594209"/>
    <nd ref="96594224"/>
    <tag k="highway" v="residential"/>
    <tag k="maxspeed" v="50"/>
    <tag k="name" v="Rockwellstraße"/>
    <tag k="oneway" v="yes"/>
    <tag k="postal_code" v="38518"/>
</way>
<way id="10522831">
    <nd ref="90664716"/>
    <nd ref="940615687"/>
    <nd ref="2222543788"/>
    <nd ref="940619729"/>
    <nd ref="90664692"/>
    <nd ref="939024170"/>
    <nd ref="298997463"/>
    <tag k="highway" v="residential"/>
    <tag k="name" v="Am Allerkanal"/>
    <tag k="postal_code" v="38518"/>
    <tag k="tracktype" v="grade2"/>
</way>

单个文件包含1000个类似的路径ID。我想将这些路径id存储在列表/元组中,但问题是路径id中的内容不是固定的


例如,“nd ref”条目的数量可以不同。我正在考虑将way id数据存储到一个元组中,并在每个元组中包含一个包含
nd ref
数据的列表。最后将所有元组存储在一个列表中。请说明这是否可行,我是否能够通过循环访问所有条目?

假设way id只有两种标记,在这种情况下,如果您希望以数据结构组织输出,将way id及其内容存储在列表中,您可以执行以下操作:

对于每个way id,您都可以定义表单的元组

 t = (way_id,[list of nd ref tags],[list of tag k values])
因此,每个路径id都有一个元组,您可以在运行时将这个元组附加到列表中。使用元组的想法更好,因为数据组织得更好,您可以非常轻松地引用元组的内容:

t[0] -> gives you the way-id
t[1] -> gives you the list of nd-ref values for that id and so on.
元组是不可变的数据结构,从这个意义上讲,一旦定义了元组(比如它的名称为“t”)。不能更改元组的内容,如:

t[0] = 34983948 /*Invalid*/

但是元组可以包含列表等可变元素。关于和的官方python文档可能也会派上用场。

thnx vivek,我将尝试这种方法