2015-06-05 42 views
0

在ArcGis中,我想計算我繪製的道路網絡的連通性。因此,根據相互交叉的街道的數量,對各道路交叉口賦予價。然而,在當前情況下,工具箱還將包含兩條線(例如以曲線形式),它們以2的價相互連接。我需要刪除價格爲< 3的列表的所有值,但是因爲我幾乎沒有編程經驗,我不知道如何正確地做到這一點。以下是用於計算頂點圖層的工具箱中的一段代碼。Python:如何從列表中刪除價值低於特定值的值

#---------------------------------- 
#Begin of calculating valence field 
gp.addmessage("Begin of calculating valence field") 
#---------------------------------- 

#Make layer of vertices 
#---------------------- 
gp.addmessage("Make layer of vertices") 
desc=gp.Describe(inline) 
shapefieldname = desc.ShapeFieldName 
thesr=desc.SpatialReference 
gp.CreateFeatureClass(gp.workspace,vert, "Point","", "ENABLED", "DISABLED", thesr) 
gp.addfield(vert, "valence", "short") 
listk=[] 
rows=gp.SearchCursor(inline) 
row = rows.Next() 
while row: 
    feat = row.GetValue(shapefieldname) 
    partnum=0 
    partcount=feat.PartCount 
    print partcount 
    while partnum < partcount: 
     part = feat.GetPart(partnum) 
     pnt = part.Next() 
     pntcount = 0 
     thex=pnt.x 
     they=pnt.y 
     thekey=(thex*1000000)+they 
     while pnt: 
      if thekey not in listk: 
       cur = gp.InsertCursor(vert) 
       rowvert = cur.NewRow() 
       rowvert.shape = pnt 
       cur.InsertRow(rowvert) 
       listk.append(thekey) 
      pnt = part.Next() 
      pntcount += 1 
     partnum += 1 
    row=rows.next() 
del row, rows, cur 

# Remove all values valence < 3 
#------------------------------- 
+1

您可能需要閱讀[問] – boardrider

+0

這個問題應該被移到http://gis.stackexchange.com/。任何人都可以爲移民投票嗎? – jotrocken

回答

2

在Python,有你可以做的值列表

# Generate a random list for example. 
import random 
some_list = random.sample(range(30), 4) 

# Keep elements greater than 3. 
filtered_list = [value for value in some_list if value >= 3] 

# The same outcome, another way to write it. 
filtered_list = filter(lambda x: x >= 3, some_list) 

# The same outcome, but written as a loop. 
filtered_list = [] 
for value in some_list: 
    if value >= 3: 
     filtered_list.append(value) 

自從我使用ArcGIS/arcpy之後已經有一段時間了,但我確信FeatureClasses不能像普通的Python lists那樣容易處理。關於你的ArcPy中的代碼,你可以使用一個UpdateCursor刪除行:

# Create an empty FeatureClass from the original. 
with arcpy.UpdateCursor(your_feature_class) as rows: 
    for row in rows: 
     if row.valence <= 2: 
      rows.deleteRow(row) 
1
你的情況

,我想創建一個只包含價> = 3的新名單:

my_list = [1, 12, 4, 3, 7, 2, 0] 
filtered_list = [val for val in my_list if val >= 3] 
print filtered_list # displays [12, 4, 3, 7] 
0

你可以,如果你使用的過濾功能

>>>filter(lambda x : x>3 , [1,2,3,4,5]) 
[4,5]