TransWikia.com

Extracting values of some columns with ArcPy

Geographic Information Systems Asked on April 22, 2021

I’ve been trying to run some code to extract the values from a column and random sample some of them.
The problem is when I try to append values from the cursor to a list.
When I append values to the list, it works, but it appends the value inside a tuple and with a comma.
Here is some sample of the code:

import arcpy
import random

arcpy.env.overwriteOutput = True
ws = r'D:Projeto_VANTSIGproc_parc.gdb'
arcpy.env.workspace = ws

teste = r'teste'
with arcpy.da.SearchCursor(teste, "ID_TALHAO") as cursor:
    list = []
    for x in cursor:
        list.append(x)
    print(list)
    sample = random.sample(list, 2)
    print(sample)

And this is the results (list of values and the sample) I got which were printed:

[(5,), (13,), (20,), (30,), (38,)]
[(30,), (13,)]

How can I extract these values and append them to a list as integer, outside the tuple and without comma?

2 Answers

list as a variable name might get you into trouble caio, try to make it a habit to prefix (myVar, pVar etc) or proper case (ProperCaseExample) your variable names otherwise you might end up overwriting a keyword. Python is lax in its protection of keywords and will not even issue a warning if you do overwrite one. Most python keywords are all lower case, True and False being notable exceptions, therefore declaring your variable names in any case other than lower case reduces the risk of problems later. Consider this:

list         # list is a keyword, python already knows what it is
<type 'list'>
list()       # returns a new, empty list but could also convert a tuple into a list
[]
list = [1,2] # oops, we've just overwritten list with a value
list         # now list is no longer <type 'list'> it's now [1,2]
[1, 2]
list()       # as list has been overwritten you can no longer use it as before
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: 'list' object is not callable

Because you've overwritten the builtin object list if you later decide to use list(myTuple) to convert a tuple into a list, a handy approach as tuples are read only and if you want to change or remove values do so as a list object then convert back to a tuple. Now that list is overwritten python is going to throw an exception and you will struggle for a very long time trying to find the error in syntax that checks out 100% with every reference and the problem could be hundreds of lines before.

Another option to append elements into a list that works for me is list += x which works if x is a tuple or list. The addition assignment operator (+=) also works for lists of more than one element:

myList = [1,2] 
myList += [3,4] 
myList is now [1, 2, 3, 4] 

Append has a different behavior with lists:

myList = [1,2] 
myList.append([3,4])
myList is now [1, 2, [3, 4]]

Which is probably not what was intended but is great if you know this quirk and are prepared for list or tuple items in your list.

In your code:

with arcpy.da.SearchCursor(teste, "ID_TALHAO") as cursor:
    list = []
    for x in cursor:
        list += x
    print(list)
    sample = random.sample(list, 2)
    print(sample)

or as a list comprehension:

list = [x[0] for x in arcpy.da.SearchCursor(teste, "ID_TALHAO")] 

Note that the addition assignment is not valid here, you need to use indexing as Hornbydd indicates in his answer.

Correct answer by Michael Stimson on April 22, 2021

Replace list.append(x) with list.append(x[0])

x is a tuple of 1 and when you add those to lists or convert to strings you get that (x,) notation.

Answered by Hornbydd on April 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP