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?

有帮助吗?

解决方案

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]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top