使用 Python 将值添加到数组并获取不同的值

发布于 2024-09-26 12:28:41 字数 963 浏览 4 评论 0原文

我下面有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-10-03 12:28:41

您可以将不同的值存储在一个集合中:

>>> 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}

您还可以使您的循环更加Pythonic,尽管我不确定为什么您要循环遍历该行(假设您没有使用它):

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

顺便说一句,“ “”文本“””不是注释。 Python 只有以 # 开头的单行注释。

You can store distinct values in a set:

>>> 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}

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):

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

And btw, """ text """ is not a comment. Python only has single line comments starting with #.

鹤仙姿 2024-10-03 12:28:41

获取不同值的一种方法是使用集合来查看您是否已经看到该值,并仅在它是新值时才显示它:

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)

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:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文