Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?
E.g., I'd like to do something like:
[ [x,2*x] for x in range(4) ]
....and receive
[ 0,0,1,2,2,4,3,6]
....but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there's some way to avoid the "lists
of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?
Thanks,
---Joel 13 1500
Joel Koltner wrote:
Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?
E.g., I'd like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there's some way to avoid the "lists
of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?
Thanks,
---Joel
-- http://mail.python.org/mailman/listinfo/python-list
For the first part:
def gen(n):
for i in xrange(n):
yield i
yield 2*i
print list(gen(4))
[0, 0, 1, 2, 2, 4, 3, 6]
gerard
Joel Koltner wrote:
Is there an easy way to get a list comprehension to produce a flat list
of, say, [x,2*x] for each input argument?
E.g., I'd like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm aware I can use any of the standard "flatten" bits of code to turn
this back into what I want, but I was hoping there's some way to avoid the
"lists of lists" generation in the first place?
>>[x*y for x in range(4) for y in 1,2]
[0, 0, 1, 2, 2, 4, 3, 6]
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip"
to do so?
>>items = [None] * 6 items[::2] = 1,2,3 items[1::2] = 4,5,6 items
[1, 4, 2, 5, 3, 6]
Peter
Joel Koltner wrote:
Is there an easy way to get a list comprehension to produce a flat list of,
say, [x,2*x] for each input argument?
E.g., I'd like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm aware I can use any of the standard "flatten" bits of code to turn this
back into what I want, but I was hoping there's some way to avoid the "lists
of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to
do so?
Thanks,
---Joel
-- http://mail.python.org/mailman/listinfo/python-list
For the first part:
def gen(n):
for i in xrange(n):
yield i
yield 2*i
print list(gen(4))
[0, 0, 1, 2, 2, 4, 3, 6]
gerard
"Joel Koltner" <za************ ***@yahoo.comwr ote in message
news:5R******** *************@e n-nntp-05.dc1.easynews .com...
Is there an easy way to get a list comprehension to produce a flat list
of, say, [x,2*x] for each input argument?
E.g., I'd like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm aware I can use any of the standard "flatten" bits of code to turn
this back into what I want, but I was hoping there's some way to avoid the
"lists of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with
list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip"
to do so?
Thanks,
---Joel
i figured out a solution
sum([x,2*x] for x in range(4)],[]) #not tested
sum(zip(list1,l ist2),()) #not tested
(you did say you knew of ways to flatten, but i dunno if you knew of that
way or not)
as an aside, i wish that sum didn't have to take the second parameter. it's
kind of superfluous. it can just use the first item as the initial value
instead of 0 when no initial value is specified. it would be a little
complex to do without putting a conditional in your main loop and slowing it
down, but it could be done.
On Thu, 22 May 2008 15:29:42 -0400, inhahe wrote:
"Joel Koltner" <za************ ***@yahoo.comwr ote in message
news:5R******** *************@e n-nntp-05.dc1.easynews .com...
>Is there an easy way to get a list comprehension to produce a flat list of, say, [x,2*x] for each input argument?
E.g., I'd like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm aware I can use any of the standard "flatten" bits of code to turn this back into what I want, but I was hoping there's some way to avoid the "lists of lists" generation in the first place?
A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" to do so?
Thanks, ---Joel
i figured out a solution
sum([x,2*x] for x in range(4)],[]) #not tested sum(zip(list1,l ist2),())
#not tested
(you did say you knew of ways to flatten, but i dunno if you knew of
that way or not)
as an aside, i wish that sum didn't have to take the second parameter.
it's kind of superfluous. it can just use the first item as the initial
value instead of 0 when no initial value is specified. it would be a
little complex to do without putting a conditional in your main loop and
slowing it down, but it could be done.
If you don't want the second parameter use reduce():
>>reduce(operat or.add, ([x, x*2] for x in xrange(4)))
[0, 0, 1, 2, 2, 4, 3, 6]
>>reduce(operat or.add, zip([1,2,3],[4,5,6]))
(1, 4, 2, 5, 3, 6)
or:
>>reduce(lamb da x, y: x.extend(y) or x, ([x, x*2] for x in xrange(4)))
[0, 0, 1, 2, 2, 4, 3, 6]
-- Ivan
Joel Koltner <za************ ***@yahoo.comwr ote:
Is there an easy way to get a list comprehension to produce a flat
list of, say, [x,2*x] for each input argument?
E.g., I'd like to do something like:
[ [x,2*x] for x in range(4) ]
...and receive
[ 0,0,1,2,2,4,3,6]
...but of course you really get a list of lists:
[[0, 0], [1, 2], [2, 4], [3, 6]]
I'm not sure I would recommend it, but try:
[v for x in range(4) for v in (x, 2 * x)]
A slightly similar problem: If I want to "merge," say, list1=[1,2,3]
with list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way
with "zip" to do so?
A similar solution as above should work.
Marc
"Peter Otten" <__*******@web. dewrote in message
news:g1******** *****@news.t-online.com...
>A slightly similar problem: If I want to "merge," say, list1=[1,2,3] ...
>>>items = [None] * 6 items[::2] = 1,2,3 items[1::2] = 4,5,6 items
[1, 4, 2, 5, 3, 6]
Thanks Peter, that's pretty clean -- I like it!
"inhahe" <in****@gmail.c omwrote in message
news:5P******** ***********@big news7.bellsouth .net...
i figured out a solution
sum([x,2*x] for x in range(4)],[]) #not tested
Nice... thanks; I probably had seen code using 'sum' to flatten but hadn't
actually understood how it worked. After playing around some it's now
clear...
(Add one more opening bracket to your solution to make the interpreter
happy -- I know you know this and it was missed only due to not actually
trying it ought)
---Joel
Hi Marc,
"Marc Christiansen" <us****@solar-empire.dewrote in message
news:39******** ****@pluto.sola r-empire.de...
I'm not sure I would recommend it, but try:
[v for x in range(4) for v in (x, 2 * x)]
That certainly works... and it almost seems like a bit less of a hack (if
perhaps somewhat harder to read) than the "sum" approach to flattening?
A similar solution as above should work.
This is what I came up with:
>>list1=[1,2,3] list2=[4,5,6] [j for (i,m) in enumerate(list1 ) for j in (m,list2[i])]
[1, 4, 2, 5, 3, 6]
Not exactly "clean" due to having to enumerate list1 to get the index for
list2. There may be a better method, of course -- so far I like Petter
Otten's approach.
Thanks for the help,
---Joel This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Fuzzyman |
last post by:
Pythons internal 'pointers' system is certainly causing me a few
headaches..... When I want to copy the contents of a variable I find
it impossible to know whether I've copied the contents *or* just
created a new pointer to the original value....
For example I wanted to initialize a list of empty lists....
a=, , , , ]
I thought there...
|
by: Stan Cook |
last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly appreciated.
x = 0
for each in _dbases:
if each <> ".dbf":
del each # also tried: del _dbases
x = x + 1
I must be doing something...
|
by: Peter |
last post by:
Hello!
Please, could anyone tell, is it possible to set multiple items to be
selected in list control in the code? For example when the web form is loaded
three items of 5 are selected in list control already? Now I manage to set
only one item to be selected during page load, but there is need to multiple
items could be selected for the...
|
by: Jed Parsons |
last post by:
Hi,
I'm using the logging module for the first time. I'm using it from
within Zope Extensions.
My problem is that, for every event logged, the logger is producing
multiple identical entries with the timestamp the same down to the
millisecond.
Is this something I'm doing wrong?
|
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 lc expression as
a for loop to drive the recursion.
Thanks for any insight!
Gregory
|
by: NeoPa |
last post by:
A number of posters have asked to be shown how to produce a list of items from multiple records which are (potentially) grouped together.
Take the following data for instance (from a table called ) :
Zone Forum
Community Introductions
Community Community Cafe
Community Software Development
Community ...
|
by: pi.arctan |
last post by:
Hi guys,
One of my many project involves working with YUV-files, where I need
to reduce
the vertical resolution with a factor of two, i.e. remove every other
scan line.
Today I'm using two for-loops in the fashion shown below
y =
for i in range(0, width*height, width*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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |