Python:如何从列表中删除化合价低于特定值的值
Python: How to remove values from a list that have a valence lower than a certain value
在 ArcGis 中,我想计算我绘制的道路网络的连通性。因此,我根据彼此相交的街道数量为每个道路交叉口赋予价。但是,在当前情况下,工具箱还将包括两条线(例如,一条曲线),它们以 2 的化合价相互连接。我需要删除列表中化合价 < 3 的所有值,但由于我几乎没有编程经验,我不知道如何正确地做到这一点。下面是工具箱中用于计算顶点层的一段代码。
#----------------------------------
#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
#-------------------------------
在你的情况下,我会创建一个新列表,只包含化合价 >= 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]
可以使用过滤功能
>>>filter(lambda x : x>3 , [1,2,3,4,5])
[4,5]
在 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. Regarding your arcpy code, you could use an 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)
在 ArcGis 中,我想计算我绘制的道路网络的连通性。因此,我根据彼此相交的街道数量为每个道路交叉口赋予价。但是,在当前情况下,工具箱还将包括两条线(例如,一条曲线),它们以 2 的化合价相互连接。我需要删除列表中化合价 < 3 的所有值,但由于我几乎没有编程经验,我不知道如何正确地做到这一点。下面是工具箱中用于计算顶点层的一段代码。
#----------------------------------
#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
#-------------------------------
在你的情况下,我会创建一个新列表,只包含化合价 >= 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]
可以使用过滤功能
>>>filter(lambda x : x>3 , [1,2,3,4,5])
[4,5]
在 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. Regarding your arcpy code, you could use an 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)