使用 Python 将值添加到数组并获取不同的值
我下面有 python 代码,它将循环遍历一个表并打印出特定列中的值。未显示的是用户选择要素图层的形式。选择要素图层后,第二个下拉列表中将填充该要素的所有列标题,用户可以选择他们想要关注的列。现在,在 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)
I have python code below that will loop through a table and print out values within a particular column. What is not shown is the form in which the user selects a Feature Layer. Once the Feature Layer is selected a second Dropdown is populated with all the Column Headings for that Feature and the user chooses which Column they want to focus on. Now within the python script, I simply print out each value within that column. But I want to store each value in a List or Array and get Distinct values. How can I do this in Python?
Also is there a more efficient way to loop through the table than to go row by row? That is very slow for some reason.
many thanks
# 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将不同的值存储在一个集合中:
您还可以使您的循环更加Pythonic,尽管我不确定为什么您要循环遍历该行(假设您没有使用它):
顺便说一句,
“ “”文本“””
不是注释。 Python 只有以#
开头的单行注释。You can store distinct values in a set:
Also you can make your loop more pythonic, although I'm not sure why you loop over the row to begin with (given that you are not using it):
And btw,
""" text """
is not a comment. Python only has single line comments starting with#
.获取不同值的一种方法是使用集合来查看您是否已经看到该值,并仅在它是新值时才显示它:
One way to get distinct values is to use a set to see if you've seen the value already, and display it only when it's a new value: