list comprehension question

Bengt Richter bokr at oz.net
Sun Mar 24 03:40:21 EST 2002


On Sat, 23 Mar 2002 23:11:58 -0700, "Andrew Dalke" <dalke at dalkescientific.com> wrote:

>Tripp Scott asked:
>>can i generate this list:
>>
>>  [1, 1.1, 2, 2.1, 3, 3.1]
>>
>>with a list comprehension in a form of this:
>>
>>  [SOMETHING for x in 1,2,3]
>>
>>and without using side effects like this?
>
>There is no way with list comprehensions which is more
>elegant than the standard solution, which is
>
>result = []
>for i in 1, 2, 3:
>    result.extend([i, i + 0.1])
>
 >>> a = [x+y for x in 1,2,3 for y in 0,0.1]
 >>> a
 [1, 1.1000000000000001, 2, 2.1000000000000001, 3, 3.1000000000000001]

And to see the elements in normal individual print precision:

 >>> map(str,a)
 ['1', '1.1', '2', '2.1', '3', '3.1']

I guess that not quite what the OP asked for, i.e., it's

    [SOMETHING for x in 1,2,3 SOMETHING_ELSE]

but no way say no way ;-)

Regards,
Bengt Richter

PS. Anyone else getting weird mail quoting a post as bounced mail, saying (in part)
--
 > >Diagnostic-Code: X-Postfix; mail for taisha.hamete.org loops back to myself
 > >Received: from fuseki.hamete.org (fuseki.hamete.org [192.168.19.1])
 > >        by localhost.hamete.org (Postfix) with ESMTP id 0F56EBD93
 > >        for <jd at taisha.hamete.org>; Sun, 24 Mar 2002 00:07:42 +0100 (CET)
 > >Received: from fuseki.hamete.org (localhost.localdomain [127.0.0.1])
 > >        by fuseki.hamete.org (Postfix) with ESMTP id AC6591E98
 > >        for <jd at taisha>; Sat, 23 Mar 2002 23:58:24 +0100 (CET)
--
I emailed the whois-indicated owner and tech support, and got an automated reply
from the latter.



More information about the Python-list mailing list