3

I am trying to get a list with a specific output from an index in another list, for example:

L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10,...etc), (...etc)]
multiple_index = [entry[0, 3, 4] for entry in L] 
#----> I know this specific code is wrong

I would love it if the above code could output:

[(0, 3, 4), (6, 9, 10), (...etc)]

I want the individual sub-indices from each index in the main list to be grouped as shown, if that is at all possible, and am wondering what code I could use to properly pull this off, thanks.

EDIT: Also, How could I format it to display as rows cleanly, I am outputting them to a text file using .writelines and a separate output line, thanks again!

2
  • Your edit makes this another question which already has plenty of answers on SO. Commented Jun 21, 2013 at 19:57
  • That's interesting, sorry I couldn't find the answer to the original question elsewhere, and the edit is just asking for an additional bit of help, that's all. Commented Jun 21, 2013 at 20:01

7 Answers 7

9

Use operator.itemgetter():

from operator import itemgetter

multiple_index = map(itemgetter(0, 3, 4), L)

or in a list comprehension:

multiple_index = [itemgetter(0, 3, 4)(i) for i in L]
Sign up to request clarification or add additional context in comments.

5 Comments

This works wonderfully, as a quick aside if you could answer this, how could I get the multiple_index to display the output in separate rows, I edited the original post with the new format :)
You can use str.join() to join lists into strings: '\n'.join([',',join(i) for i in multiple_index]) for one string joining the outer lists by newlines, inner lists by comma.
It gives me an invalid syntax error when I apply the above code? Any idea why?
@ImmortalxR: Because I accidentally typed a comma where a full-stop goes. You also have integer inputs, so you'd need to map to str first: '\n'.join([','.join(map(str, i)) for i in multiple_index])
Thanks! I had a feeling we might have had a typo in there haha
3

Here is one option:

L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11), (11, 12, 13, 14, 15, 16)]
multiple_index = [(entry[0], entry[3], entry[4]) for entry in L] 

Or using operator.itemgetter():

from operator import itemgetter
indices = itemgetter(0, 3, 4)
multiple_index = [indices(entry) for entry in L] 

Comments

2

Are You interested in this?

L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10,...etc), (...etc)]
multiple_index = [(entry[0], entry[3], entry[4]) for entry in L] 
#----> I know this specific code is wrong

Comments

2
from operator import itemgetter
get = itemgetter(0, 3, 4)
L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10,...etc), (...etc)]
multiple_index = [get(entry) for entry in L]

for a more functional style:

multiple_index = map(itemgetter(0, 3, 4), L)

Of course, if you're using numpy, you could do something like the following:

import numpy as np
L = np.array([(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11), (11, 12, 13, 14, 15, 16)])
multiple_index = L[:,(0, 3, 4)]

resulting in:

array([[ 0,  3,  4],
       [ 6,  9, 10],
       [11, 14, 15]])

Personally, I like the numpy version the best, but that requires you to have numpy installed. Here's some more on numpy indexing if you're interested: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

Numpy also has some neat shortcuts/tricks for fancy slice and range building using np.s_, np.r_, and np.c_.

Comments

2

Just for some diversity, here's a way with itertools.compress,

>>> from itertools import compress, count
>>> indices = {0,3,4}
>>> items_at = lambda indices: (1 if n in indices else 0 for n in count())
>>> [tuple(compress(e, items_at(indices))) for e in L]
[(0, 3, 4), (6, 9, 10)]

Comments

0

list tuple and dictioonary lookups are implemented using their getitem methods

myarray=[0,1,2]
print myarray[1]
#result:1
#equivalent to
print myarray.__getitem__(1)

you can map the indices you want through each list's getitem function. This returns a list containing the items at those indices for each list. Modifying your example code:

L = [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10,...etc), (...etc)]
multiple_index = [map(entry.__getitem__,[0, 3, 4]) for entry in L]

this produces the desired output.

for more on python's magic methods see this:

Comments

0

Here is how I would do it:

    L=[tuple(range(0,6*1)),tuple(range(6*1,6*2)),tuple(range(6*2,6*3))]
    print [tuple(map(lambda i: entry[i],[0,3,4])) for entry in L]

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.