附加到一个元组覆盖previous值 [英] Appending to a tuple overwriting previous values

查看:203
本文介绍了附加到一个元组覆盖previous值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ArcPy中获得的形状文件的所有折线。 SearchCursor 返回游标,这样我可以遍历形文件上的所有功能。问题是,我想节省光标以备后用返回的所有对象。

I'm using arcpy to get all the polylines of a shape file. SearchCursor returns a cursor so that I can iterate over all the features of shape file. Problem is I want to save all the objects returned by cursor for later use.

import arcpy
from arcpy import env

env.workspace = r"C:\GIS Data\GIS data"

desc = arcpy.Describe("River.shp")
shapefieldname = desc.ShapeFieldName

rows = arcpy.SearchCursor("River.shp")

featureList = ()

for row in rows:
    feat = row.getValue(shapefieldname)

    featureList = featureList + (feat, )

    print "%i %i" % (featureList[-1].firstPoint.X, featureList[-1].firstPoint.Y)
    print "%i %i" % (featureList[-1].lastPoint.X, featureList[-1].lastPoint.Y)

    print

print "---------------------------------------------------------------"

for feat in featureList:
    print "%i %i" % (feat.firstPoint.X, feat.firstPoint.Y)
    print "%i %i" % (feat.lastPoint.X, feat.lastPoint.Y)
    print

元组应该包含由光标返回的所有对象。但它只有最后一个元素重复尺寸次元组数。

3610930 2135882 3611593 2134453

3610930 2135882 3611593 2134453

3611806 2134981 3611593 2134453

3611806 2134981 3611593 2134453

3614160 2136164 3617432 2131734

3614160 2136164 3617432 2131734

3611593 2134453 3617432 2131734

3611593 2134453 3617432 2131734

3617432 2131734 3620568 2127591

3617432 2131734 3620568 2127591

3620568 2127591 3620785 2127423

3620568 2127591 3620785 2127423

3617980 212665​​7 3620568 2127591

3617980 2126657 3620568 2127591

3616768 2129454 3617948 2126649

3616768 2129454 3617948 2126649

3617948 2126649 3617980 212665​​7

3617948 2126649 3617980 2126657

3615102 2128889 3617587 2126510

3615102 2128889 3617587 2126510

3617587 2126510 3617948 2126649

3617587 2126510 3617948 2126649

3617624 2126416 3617980 212665​​7

3617624 2126416 3617980 2126657

3613129 2128176 3615155 2125617

3613129 2128176 3615155 2125617

3615155 2125617 3617587 2126510

3615155 2125617 3617587 2126510

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

起初,我尝试使用这个列表。相同的输出也被发现名单时,我用'追加()方法。由于元组是不可变的数据结构,如何能 + 覆盖元组的所有previous元素。虽然这code是ArcPy中写的,但我想这个问题是不特定的ArcGIS

At first, I've tried this using list. Same output was also found for list when I've used 'append()' method. As tuple is immutable data structure, how can + overwrites all the previous elements of tuple. Although this code is written for arcpy, but I guess the problem isn't arcgis specific.

推荐答案

什么这表明的是, row.getValue()保持返回引用的同一个对象的,它在不断更新的地方。​​

What this suggests is that row.getValue() keeps returning references to the same object, which it keeps updating in place.

要验证,请尝试打印 ID(技艺) ID(feat.firstPoint)编号(feat.lastPoint)在第一循环,并且看到任何的id是否仍迭代之间相同。如果其中的任何事,那是你的问题。

To verify, try printing id(feat), id(feat.firstPoint) and id(feat.lastPoint) in the first loop, and see whether any of the ids remain the same between iterations. If any of them do, that's your problem.

由于元组是不可变的数据结构,如何能+覆盖元组的所有previous元素。

As tuple is immutable data structure, how can + overwrites all the previous elements of tuple.

它没有。元组的,你不能添加或删除它的元素,而无需创建一个新的记录感不可改变的。您也不能更改元组元素的值。但是,如果该元素是一个可变对象的引用,你可以自由地修改对象本身。这就是我怀疑这里发生的事情:你有相同的对象多个引用;当你修改一个,他们都出现改变。

It doesn't. Tuple is immutable in the sense that you can't add or remove elements from it without creating a new tuple. You also can't change the value of a tuple element. However, if that element is a reference to a mutable object, you are free to modify the object itself. This is what I suspect is happening here: you have multiple references to the same object; when you modify one, they all appear to change.

这篇关于附加到一个元组覆盖previous值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆