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 4 1448
Marco Aschwanden <PP**********@spammotel.com> wrote:
: 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[i] for i in columns]))
###
For example:
### l = [['1', '2', '3'],
.... ['a', 'b', 'c'],
.... ['ka', 'na', 'da']] for row in cut(l, (2, 0, 1)): print row
....
('3', '1', '2')
('c', 'a', 'b')
('da', 'ka', 'na') for row in cut(l, (2, 1)): print row
....
('3', '2')
('c', 'b')
('da', 'na')
###
Hope this helps!
Marco Aschwanden <PP**********@spammotel.com> wrote: 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
Marco Aschwanden <PP**********@spammotel.com> wrote in message news:<ma**************************************@pyt hon.org>... 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[i] for i in cols] for row in theList]
new_list
[[11, 1], [22, 2], [33, 3]]
?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David |
last post by:
I would like to be able to re-sort data in an HTML table on the
without returning to the server. It seems like an XSLT should be able
to accomplish this, but I can't find enough information...
...
|
by: N. Demos |
last post by:
I'm having problems with a custom JS object (XMLLoadObject) I designed
to load XML and XSL files, perform an XSL transform with them and embed
the resultant HTML fragment into the host HTML...
|
by: Patrick Guio |
last post by:
Dear all,
I am trying to use the std::transform algorithm to to the following
vector< vector<char> >::iterator ik = keys.begin(); // key list iterator
vector< vector<char> >::iterator is = ik;...
|
by: Doug Heeren |
last post by:
I have the following section of VB.NET code that transforms a simple dataset
into an Excel xml workbook. It works fine for < 50 rows or so, but I have
about 8,000 rows I need to transform. Is there...
|
by: steve |
last post by:
I am trying to create an XSLT Transform but keep getting the same problem.
Overload resolution failed because no accessible 'Transform' can be called
with these arguments.
I create a reference...
|
by: ibiza |
last post by:
Hi all,
I have a question which I have no ideal of the answer...I am currently
working on a web application and at some time, I have a string
representing a short text. This could be a simple...
|
by: BBM |
last post by:
I have the following base class that uses Generics in its definition.
MyList(of T, C) - T is a BindingList(Of C), C is the type
in the list
I have many implemented classes...
|
by: KevinADC |
last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
|
by: Chris Rebert |
last post by:
On Wed, Sep 24, 2008 at 2:02 PM, David Di Biase <dave.dibiase@gmail.comwrote:
You'd probably be better off using the 'key' keyword argument to
..sort(), which is made for the Schwartzian...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |