0

I know i can iterate over an array and append the values at each index to my 4 array variables, but is there a quicker, less coding way to do this? I am wonder if i can do something like this (which obviously doesn't work):

tupleOfArrays = ([['s1',2,3], 
                  ['s2',3,4],
                  ['s5',6,7]])

arr1, arr2, arr3 = [(a[0],a[1],a[2]) for a in tupleOfArrays]

at the end of this, the three arrays should contain:

  arr1=['s1',2,5]
  arr2=['s2',3,6]
  arr3=['s3',4,7]

Edited: i've edited this to reflect what my structure 'truly' looks like so that the selected answer makes more sense. that i have is an tuple containing an list of list. first element of the inner list are strings.

Thanks much

3 Answers 3

3

First, try to use Python nomenclature - what you have is a tuple of lists. Also, names in Python (except class names) are usually lowercase_with_underscores.

To answer your actual question, use zip to combine items at the same indices in multiple collections:

lst1, lst2, lst3 = map(list, zip(*tuple_of_lists))

zip produces tuples by default, so I've used map to convert them to lists.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, i get a "zip argument #2 must support iteration" when i try this solution
This is a nice standard-library-only solution. It works for me - @mike01010, try using this exact code with tupleOfArrays created exactly as in your question: arr1, arr2, arr3 = map(list, zip(*tupleOfArrays))
yes. i actually had a list of list, which easily made tuple of list. unfortunately the new error is 'too many items to unpack'. there are about 700 entries and the array had 6 indices.
doing something like this worked for me: lst1, lst2, lst3 = zip(*tuple_of_lists[0])
1

If you're willing to use numpy,

import numpy as np

tupleOfArrays = ([1,2,3], 
                 [2,3,4],
                 [5,6,7])

toa = np.array(tupleOfArrays)

arr1 = toa[:,0]
arr2 = toa[:,1]
arr3 = toa[:,2]

6 Comments

can you apply a converter to this? to convert string values in array to numbers say?
if you are using numpy already, why not just use numpy.transpose()?
Yeah, you could, but for this application you still have to index the array at the end, so this seemed simpler. Seems likely the computation for 2nd-index slicing vs. transpose + 1st-index slicing is pretty similar.
@mike01010 Not sure what you mean. I'd either modify your question above, or ask a separate question.
yeah..i think i tried to simplify it too much. the first index in the list contain strings, while the rest are floats. it seems numpy.array will use that first index as the target type for all items when converting the list to a numpy array.
|
1

If you use numpy you can use the transpose method to get what you are looking for:

import numpy as np

tupleOfArrays = ([1,2,3], 
                 [2,3,4],
                 [5,6,7])

numpyList=np.array(tupleOfArrays)
arr1,arr2,arr3 = numpyList.transpose()

Also, to answer your comment about changing values from string to int, you can use the numpy method astype:

import numpy as np       

tupleOfArrays = (["1","2","3"], 
                     ["2","3","4"],
                     ["5","6","7"])

numpyList=np.array(tupleOfArrays)
transposed = numpyList.transpose()
arr1,arr2,arr3 = transposed.astype(np.int)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.