How to generically transform a list?

M

Marco Aschwanden

Suppose you have a list of lists:

theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]

I would like to have a GENERIC way how to turn this list of list into
another list of list.
- A user can choose which columns she wants
- A user can select the order of the columns

For example:
The user wants columns: 1,2
The user wants it to be ordered: 2,1

A non generic approach would maybe do the following:
theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]
new_list = [[row[2], row[1]] for row in theList]
new_list
[[11, 1], [22, 2], [33, 3]]

I am sure there must be a rather elegant generic approach, which is
lurking somewhere to be realeased.

Thanks for any hint in advance,
Marco
 
D

Daniel Yoo

: Suppose you have a list of lists:

: theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]

: I would like to have a GENERIC way how to turn this list of list into
: another list of list.
: - A user can choose which columns she wants
: - A user can select the order of the columns


It sounds like you want something like the 'cut' Unix command. Here's
one way to do it.

###
def cut(iterable, columns):
"""Selects columns from the iterable."""
for row in iter(iterable):
yield(tuple([row for i in columns]))
###



For example:

###
.... ['a', 'b', 'c'],
.... ['ka', 'na', 'da']]....
('3', '1', '2')
('c', 'a', 'b')
('da', 'ka', 'na')....
('3', '2')
('c', 'b')
('da', 'na')
###


Hope this helps!
 
A

Alex Martelli

Marco Aschwanden said:
Suppose you have a list of lists:

theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]

I would like to have a GENERIC way how to turn this list of list into
another list of list.
- A user can choose which columns she wants
- A user can select the order of the columns

For example:
The user wants columns: 1,2
The user wants it to be ordered: 2,1

def GENERIC(list_of_lists, user_wants):
return [ [row[x] for x in user_wants] for row in list_of_lists ]

example use:

print GENERIC(theList, (2, 1))

emits

[[11, 1], [22, 2], [33, 3]]

I am sure there must be a rather elegant generic approach, which is
lurking somewhere to be realeased.

Funny enough, just this afternoon I was editing a "reordering lists of
lists" recipe for the cookbook's 2nd edition (at the mall, on my iBook,
sitting at a cafe, drinking Schweppes Orange and smoking, while my wife
and co-editor Anna did the grocery shopping -- later she was at the cafe
while I went shopping at the pharmacy, evening things out;-),
and exactly this one came up -- the original author suggested a
hardcoded approach and I widened it up to just this GENERIC function
(with a better name, to be sure, but you insisted;-). Which is why I
still have it topmost in my mind right now (judging from your post's
timestamp you posted just as I was working on this very recipe, funny
coincidence).

It's one candidate for the 'shortcuts' chapter and there's more stuff
slated for that chapter that we can possibly choose for publication, but
it's interesting info for me that this specific task IS interesting to
somebody -- thanks!-)


Alex
 
N

Neal Holtz

Marco Aschwanden said:
Suppose you have a list of lists:

theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]

I would like to have a GENERIC way how to turn this list of list into
another list of list.
- A user can choose which columns she wants
- A user can select the order of the columns

For example:
The user wants columns: 1,2
The user wants it to be ordered: 2,1

A non generic approach would maybe do the following:
theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]
new_list = [[row[2], row[1]] for row in theList]
new_list
[[11, 1], [22, 2], [33, 3]]

I am sure there must be a rather elegant generic approach, which is
lurking somewhere to be realeased.

Thanks for any hint in advance,
Marco

theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]
cols = [2,1]
new_list = [[row for i in cols] for row in theList]
new_list
[[11, 1], [22, 2], [33, 3]]

?
 
A

Arthur Rambo

As simple as

theList=[['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]
returnList=[2,1]

newList=[[item[index] for index in returnList] for item in theList]

Python rules...
Suppose you have a list of lists:

theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]

I would like to have a GENERIC way how to turn this list of list into
another list of list.
- A user can choose which columns she wants
- A user can select the order of the columns

For example:
The user wants columns: 1,2
The user wants it to be ordered: 2,1

A non generic approach would maybe do the following:
theList = [['a',1,11,'aa'], ['b',2,22,'bb'],['c',3,33,'cc']]
new_list = [[row[2], row[1]] for row in theList]
new_list
[[11, 1], [22, 2], [33, 3]]

I am sure there must be a rather elegant generic approach, which is
lurking somewhere to be realeased.

Thanks for any hint in advance,
Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top