Help doing it the "python way"
Ian Kelly
ian.g.kelly at gmail.com
Fri May 25 11:31:31 EDT 2012
On Thu, May 24, 2012 at 5:12 PM, Emile van Sebille <emile at fenx.com> wrote:
> On 5/24/2012 2:30 PM Paul Rubin said...
>
>> Paul Rubin<no.email at nospam.invalid> writes:
>>>
>>> new_list = chain( ((x,y-1), (x,y+1)) for x,y in coord_list )
>>
>>
>> Sorry:
>>
>> new_list = list(chain( ((x,y-1), (x,y+1)) for x,y in coord_list))
>
>
>
>>>> from itertools import chain
>>>> coord_list = zip(range(20,30),range(30,40))
>>>> a = [((x,y-1),(x,y+1)) for x,y in coord_list]
>>>> b = list(chain(((x,y-1),(x,y+1)) for x,y in coord_list))
>>>> a == b
> True
>>>>
>
> So, why use chain? Is it a premature optimization? Similar to the practice
> of using "".join(targets) vs targeta+targetb+...+targetn?
Paul's code is incorrect. It should use chain.from_iterable in place
of chain. The difference then is that it creates a single list of
coordinates, rather than a list of pairs of coordinates.
Cheers,
Ian
More information about the Python-list
mailing list