1

I am running a function developed by Esri to get list of values in a integer column of a spatial table (however, the same behaviour is observed even when running the function on a non-spatial table). According to the help, I should get NumPy structured array. After running the function, I have a numpy array. I run print in this format:

in_table = r"C:\geodb101@server.sde\DataTable" #
data = arcpy.da.TableToNumPyArray(in_table, "Field3")
print data

Which gives me back this in IDE (copy/pasted from IDE interpreter):

[(20130825,) (20130827,) (20130102,)]

I am running:

allvalues = data.tolist()

and getting:

[(20130825,), (20130827,), (20130102,)]

Same result when running data.reshape(len(data)).tolist() as suggested in comments.

Running type() lets me know that in the first case it is <type 'numpy.ndarray'> and in the second case <type 'list'>. I am expecting to get my output list in another format [20130825, 20130827, 20130102]. What am I doing wrong or what else should I do to get the output list in the specified format?

YXD
  • 31,741
  • 15
  • 75
  • 115
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
  • 1
    What? `[20130825, (20130827, 20130102]` is not a format. It's a `SyntaxError`. Did you mean the list `[20130825, 20130827, 20130102]`? When writing: `[(20130825,) (20130827,) (20130102,)]` do you mean you have an array with `Nx1` dimensions? In this case you could just reshape it: `data.reshape(len(data)).tolist()`. – Bakuriu Mar 13 '14 at 12:17
  • 2
    Copy-paste your relevant code and output, to avoid having us guessing as Bakuriu indicates above. –  Mar 13 '14 at 12:36
  • How did you end up with a numpy array of tuples? What code are you using to generate that? – YXD Mar 13 '14 at 13:14
  • Added more details and fixed the typo – Alex Tereshenkov Mar 13 '14 at 15:02

1 Answers1

4

I have a possible approach, but I'm not 100% sure it will work, as I can't figure out how you got tuples into an array (when I tried to create an array of tuples, it looks like the tuples got converted to arrays). In any case, give this a shot:

my_list = map(lambda x: x[0], my_np_array_with_tuples_in_it)

This assumes you're dealing specifically with the single element tuples you describe above. And like I said, when I tried to recreate your circumstances, numpy did some conversion moves that I don't fully understand (not really a numpy expert).

Hope that helps.

Update: Just saw the new edits. Not sure if my answer applies anymore.

Update 2: Glad that worked, here's a bit of elaboration.

Lambda is basically just an inline function, and is a construct common in a lot of languages. It's essentially a temporary, anonymous function. You could have just as easily done something like this:

def my_main_func():
    def extract_tuple_value(tup):
        return tup[0]

    my_list = map(extract_tuple_value, my_np_array_with_tuples_in_it)

But as you can see, the lambda version is more concise. The "x" in my initial example is the equivalent of "tup" in the more verbose example.

Lambda expressions are generally limited to very simple operations, basically one line of logic, which is what is returned (there is no explicit return statement).

Update 3: After chatting with a buddy and doing some research, list comprehension is definitely the way to go (see Python List Comprehension Vs. Map).

From acushner's comment below, you can definitely go with this instead:

my_list = [tup[0] for tup in my_np_array_with_tuples_in_it]
Community
  • 1
  • 1
Nacho
  • 451
  • 2
  • 9
  • Yeah, this looked also to me like a tuple with single element (123,) where after comma (,) there was no element. The piece of code applied on my numpy array gives me back [20130825, 20130827, 20130102] as I need. Would you explain a bit more what happens within this chunk of code? I am not really familiar with lambda. – Alex Tereshenkov Mar 13 '14 at 15:24
  • Sure, I'll put it in the main answer above. Again, not super familiar with numpy, but I should be able to clarify the standard Python stuff. – Nacho Mar 13 '14 at 15:25
  • Thanks! Added a little more clarification that I just thought of. – Nacho Mar 13 '14 at 15:37
  • 1
    another way is just [x[0] for x in l] – acushner Mar 13 '14 at 18:24
  • 1
    Way cleaner, for some reason didn't think of list comprehension when looking at the arrays. – Nacho Mar 13 '14 at 18:26