Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can't find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Thanks,
Geoffrey 12 2263
On Aug 23, 9:24 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can't find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Suppose f is:
>>def f(n): return str(n)
>>import itertools
Using a list comprehension:
>>[i for i in itertools.chain(*[(eachx, [f(y) for y in eachx]) for eachx in x])]
[[1, 2], ['1', '2'], [3, 4], ['3', '4']]
Using a list:
>>list(itertools.chain(*[(eachx, [f(y) for y in eachx]) for eachx in x]))
[[1, 2], ['1', '2'], [3, 4], ['3', '4']]
Not so pretty in either case.
--
Hope this helps,
Steven
beginner wrote:
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can't find any way to do that with list comprension.
>>[a for b in ((item, map(f, item)) for item in x) for a in b]
[[1, 2], [f(1), f(2)], [3, 4], [f(3), f(4)]]
I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Using a loop gives you clearer code in this case, so I'd use that.
Peter
On Aug 23, 9:24 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can't find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Thanks,
Geoffrey
This may be what you want:
l = [[y, [f(z) for z in y]] for y in x]
But It's a bit dense. How about :
l=[]
for y in x:
Fy = [f(z) for z in y]
l.extend([y, Fy])
-- David
On Aug 24, 12:41 am, Davo <davb...@gmail.comwrote:
On Aug 23, 9:24 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can't find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Thanks,
Geoffrey
This may be what you want:
l = [[y, [f(z) for z in y]] for y in x]
But It's a bit dense. How about:
l=[]
for y in x:
Fy = [f(z) for z in y]
l.extend([y, Fy])
-- David- Hide quoted text -
- Show quoted text -
This is exactly what I was looking for. Thanks.
beginner <zy*******@gmail.comwrites:
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[[a, map(f,a)] for a in x]
Paul Rubin wrote:
beginner <zy*******@gmail.comwrites:
>For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[[a, map(f,a)] for a in x]
no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3), f(4)]]]
eg two sublists instead of four.
[map(g,a) for a in x for g in [None,f]]
will do it.
....a bit too cleverly, but there's worse :
list((yield a) or map(f,a) for a in x)
Cheers, BB
Boris Borcic <bb*****@gmail.comwrites:
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[[a, map(f,a)] for a in x]
no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3), f(4)]]]
eg two sublists instead of four.
Oh ugh, I misread the original request and thought the extra brackets
were there. I think the following works and is reasonably intuitive:
from itertools import chain
y = list(chain(*([a, map(f,a)] for a in x)))
But the original request itself seems a bit weird.
On Aug 24, 12:44 am, beginner <zyzhu2...@gmail.comwrote:
On Aug 24, 12:41 am, Davo <davb...@gmail.comwrote:
On Aug 23, 9:24 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can't find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Thanks,
Geoffrey
This may be what you want:
l = [[y, [f(z) for z in y]] for y in x]
But It's a bit dense. How about:
l=[]
for y in x:
Fy = [f(z) for z in y]
l.extend([y, Fy])
-- David- Hide quoted text -
- Show quoted text -
This is exactly what I was looking for. Thanks.- Hide quoted text -
- Show quoted text -
On second thought, that will generate one additional level. So it is
not what I am looking for.
On Aug 24, 5:47 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
Boris Borcic <bbor...@gmail.comwrites:
>For example, if I have x=[ [1,2], [3,4] ]
>What I want is a new list of list that has four sub-lists:
>[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[[a, map(f,a)] for a in x]
no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3), f(4)]]]
eg two sublists instead of four.
Oh ugh, I misread the original request and thought the extra brackets
were there. I think the following works and is reasonably intuitive:
from itertools import chain
y = list(chain(*([a, map(f,a)] for a in x)))
But the original request itself seems a bit weird.
Not so werid. :-) Just to put this into context, I have a list of list
of objects x=[[o1, o2, o3, o4], [o5,o6]]
I was trying to plot them with matplotlib. Each sub-list is a line. So
I would have to re-organize the list to be like the following:
v=[[o1.x, o2.x, o3.x, o4.x], [o1.y, o2.y, o3.y, o4.y], [o5.x, o6.x],
[o5.y, o6.y]]
So that I can pass it to plot:
plot(*tuple(v))
I like the chain and map solutions.
Boris Borcic wrote:
>>For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[[a, map(f,a)] for a in x]
[map(g,a) for a in x for g in [None,f]]
will do it.
...a bit too cleverly, but there's worse :
list((yield a) or map(f,a) for a in x)
worse (in *many* ways) solutions:
l = [[a, map(f,a)] for a in x]
1) s = sum(l, [])
2) from operator import add
r = reduce(add, l, [])
3) a = []
for e in l: a.extend(e)
--
Under construction
For example, if I have x=[ [1,2], [3,4] ]
>
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[[a, [f(b) for b in a]] for a in x]
On Aug 24, 5:35 am, Boris Borcic <bbor...@gmail.comwrote:
Paul Rubin wrote:
beginner <zyzhu2...@gmail.comwrites:
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[[a, map(f,a)] for a in x]
no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3), f(4)]]]
eg two sublists instead of four.
[map(g,a) for a in x for g in [None,f]]
will do it.
...a bit too cleverly, but there's worse :
list((yield a) or map(f,a) for a in x)
Cheers, BB
Really cool! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Moosebumps |
last post by:
Does anyone here find the list comprehension syntax awkward?
I like it because it is an expression rather than a series of statements,
but it is a little harder to maintain it seems.
e.g. you...
|
by: Chris P. |
last post by:
Hi. I've made a program that logs onto a telnet server, enters a
command, and then creates a list of useful information out of the
information that is dumped to the screen as a result of the...
|
by: Mahesh Padmanabhan |
last post by:
Hi,
When list comprehension was added to the language, I had a lot of
trouble understanding it but now that I am familiar with it, I am not
sure how I programmed in Python without it.
Now I...
|
by: Darren Dale |
last post by:
Hi,
I need to replace the following loop with a list comprehension:
res=
for i in arange(10000):
res=res+i
In practice, res is a complex 2D numarray. For this reason, the regular
output...
|
by: a |
last post by:
can someone tell me how to use them
thanks
|
by: Gregory Guthrie |
last post by:
Sorry for a simple question- but I don't understand how to parse this use of
a list comprehension.
The "or" clauses are odd to me.
It also seems like it is being overly clever (?) in using a...
|
by: bullockbefriending bard |
last post by:
Given:
class Z(object):
various defs, etc.
class ZList(list):
various defs, etc.
i would like to be able to replace
|
by: beginner |
last post by:
Hi All,
If I have a list comprehension:
ab=
c = "ABC"
print c
|
by: idiolect |
last post by:
Hi all - Sorry to plague you with another newbie question from a
lurker. Hopefully, this will be simple.
I have a list full of RGB pixel values read from an image. I want to
test each RGB band...
|
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: 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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
| |