2010-10-07 53 views
2

我有下面的python代碼,它將通過表循環並打印出特定列中的值。未示出的是用戶選擇特徵層的形式。選擇要素圖層後,會爲該要素填充第二個下拉列表的所有列標題,然後用戶選擇要關注的列。現在在Python腳本中,我只是打印出該列中的每個值。但我想要將每個值存儲在List或Array中並獲取Distinct值。我怎樣才能在Python中做到這一點?將值添加到數組並使用Python獲取不同的值

還有一種更有效的方式來遍歷表,而不是逐行?出於某種原因,這非常緩慢。

千恩萬謝

# Import system modules 
import sys, string, os, arcgisscripting 

# Create the Geoprocessor object 
gp = arcgisscripting.create(9.3) 
gp.AddToolbox("E:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx") 

# Declare our user input args 
input_dataset = sys.argv[1] #This is the Feature Layer the User wants to Query against 
Atts = sys.argv[2]   #This is the Column Name The User Selected 

#Lets Loop through the rows to get values from a particular column   

fc = input_dataset 

gp.AddMessage(Atts) 

rows = gp.searchcursor(fc) 
row = rows.next() 
NewList = [] 

for row in gp.SearchCursor(fc): 
    ##grab field values 
    fcValue = fields.getvalue(Atts) 
    NewList.add(fcValue) 

回答

3

您可以將不同的值在一組:

>>> a = [ 1, 2, 3, 1, 5, 3, 2, 1, 5, 4 ] 
>>> b = set(a) 
>>> b 
{1, 2, 3, 4, 5} 
>>> b.add(5) 
>>> b 
{1, 2, 3, 4, 5} 
>>> b.add(6) 
>>> b 
{1, 2, 3, 4, 5, 6} 

你也可以讓你的循環更Python,雖然我不知道爲什麼你遍歷行開始與(假設你不使用它):

for row in gp.searchcursor(fc): 
    ##grab field values 
    fcValue = fields.getvalue(Atts) 
    gp.AddMessage(fcValue) 

而且順便說一句,""" text """不是評論。 Python只有從#開始的單行註釋。

+0

埃。使用'set',你會一切設置。 +1。 – 2010-10-07 16:54:40

+0

嗨戳,我改變了我的代碼,以反映你的調整,我得到一個錯誤。你看到上面有什麼問題嗎?我現在把set()留出來,直到我得到循環工作 – Josh 2010-10-07 18:31:59

+0

我想知道的是(a)爲什麼你迭代'row',(b)'fields'是什麼(因爲你沒有定義它所示的代碼,但我猜想它來自其他地方)和(三)你可能會遇到問題,因爲你調用兩次'searchcursor';這可能會發生衝突,所以最好先刪除第一個電話。 – poke 2010-10-07 18:48:42

1

一種方式來獲得不同的值是使用一組看到,如果你已經看到了價值已經,並顯示它只有當它是一個新的價值:

fcValues = set() 
for row in gp.searchcursor(fc): 
    ##grab field values 
    fcValue = fields.getvalue(Atts) 
    if fcValue not in fcValues: 
     gp.AddMessage(fcValue) 
    fcValues.add(fcValue) 
相關問題