[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Walking lists

lallous

2/25/2010 12:28:00 PM

Hello


I am still learning Python, and have a question, perhaps I can shorten
the code:

L = (
(1, 2, 3),
(4,),
(5,),
(6, 7)
)

for x in L:
print x

What I want, is to write the for loop, something like this:

for (first_element, the_rest) in L:
print first_element
for x in the_rest:
# now access the rest of the elements

I know I can :
for x in L:
first = x[0]
rest = x[1:]
....
Probably that is not possible, but just asking.

Thanks,
Elias
6 Answers

Peter Otten

2/25/2010 12:45:00 PM

0

lallous wrote:

> I am still learning Python, and have a question, perhaps I can shorten
> the code:
>
> L = (
> (1, 2, 3),
> (4,),
> (5,),
> (6, 7)
> )
>
> for x in L:
> print x
>
> What I want, is to write the for loop, something like this:
>
> for (first_element, the_rest) in L:
> print first_element
> for x in the_rest:
> # now access the rest of the elements
>
> I know I can :
> for x in L:
> first = x[0]
> rest = x[1:]
> ....
> Probably that is not possible, but just asking.

In Python 3 you can write

>>> for first, *rest in L:
.... print("first:", first, "rest:", rest)
....
first: 1 rest: [2, 3]
first: 4 rest: []
first: 5 rest: []
first: 6 rest: [7]

Peter

Tim Chase

2/25/2010 1:03:00 PM

0

lallous wrote:
> L = (
> (1, 2, 3),
> (4,),
> (5,),
> (6, 7)
> )
>
> What I want, is to write the for loop, something like this:
>
> for (first_element, the_rest) in L:
> print first_element
> for x in the_rest:
> # now access the rest of the elements

Python 3 introduced a variable tuple assignment which I
suspect[*] would work in this context:

for first, *rest in L: # note the asterisk
print first
for x in rest:
do_stuff(x)

> I know I can :
> for x in L:
> first = x[0]
> rest = x[1:]

However in 2.x, this is the way to do it. Though if you want to
abstract the logic, you can move it to a generator:

def splitter(i):
for t in i:
yield t[0], t[1:]

for first, rest in splitter(L):
print first
for x in rest:
do_stuff(x)

-tkc



[*] not having py3 on this machine, I can't readily verify this.






Arnaud Delobelle

2/25/2010 1:27:00 PM

0



lallous wrote:
> Hello
>
>
> I am still learning Python, and have a question, perhaps I can shorten
> the code:
>
> L = (
> (1, 2, 3),
> (4,),
> (5,),
> (6, 7)
> )
>
> for x in L:
> print x
>
> What I want, is to write the for loop, something like this:
>
> for (first_element, the_rest) in L:
> print first_element
> for x in the_rest:
> # now access the rest of the elements
>
> I know I can :
> for x in L:
> first = x[0]
> rest = x[1:]

Others have replied about Python 3. In Python 2.x, you can use an
iterator:

for tuple in L:
it = iter(tuple)
first = it.next()
for x in it:
...

HTH

--
Arnaud

lallous

2/25/2010 1:30:00 PM

0

Thank you all for the replies.

The solution using Python 3's syntax look very intuitive.

Thanks Tim, Arnaud for the idea (I am using 2.x)

--
Elias
On Feb 25, 1:28 pm, lallous <elias.bachaal...@gmail.com> wrote:
> Hello
>
> I am still learning Python, and have a question, perhaps I can shorten
> the code:
>
> L = (
>   (1, 2, 3),
>   (4,),
>   (5,),
>   (6, 7)
> )
>
> for x in L:
>     print x
>
> What I want, is to write the for loop, something like this:
>
> for (first_element, the_rest) in L:
>   print first_element
>   for x in the_rest:
>     # now access the rest of the elements
>
> I know I can :
> for x in L:
>     first = x[0]
>     rest = x[1:]
>     ....
> Probably that is not possible, but just asking.
>
> Thanks,
> Elias

Jean-Michel Pichavant

2/25/2010 2:28:00 PM

0

lallous wrote:
> Thank you all for the replies.
>
> The solution using Python 3's syntax look very intuitive.
>
> Thanks Tim, Arnaud for the idea (I am using 2.x)
>
> --
> Elias
> On Feb 25, 1:28 pm, lallous <elias.bachaal...@gmail.com> wrote:
>
>> Hello
>>
>> I am still learning Python, and have a question, perhaps I can shorten
>> the code:
>>
>> L = (
>> (1, 2, 3),
>> (4,),
>> (5,),
>> (6, 7)
>> )
>>
>> for x in L:
>> print x
>>
>> What I want, is to write the for loop, something like this:
>>
>> for (first_element, the_rest) in L:
>> print first_element
>> for x in the_rest:
>> # now access the rest of the elements
>>
>> I know I can :
>> for x in L:
>> first = x[0]
>> rest = x[1:]
>> ....
>> Probably that is not possible, but just asking.
>>
>> Thanks,
>> Elias
>>
Using slicing + list comprehension with python 2.x

for first, rest in [(e[0],e[1:]) for e in L]:
print first
print rest


1
(2, 3)
4
()
5
()
6
(7,)


But honestly, the code you provided is just fine.

JM

Mensanator

2/25/2010 7:00:00 PM

0

On Feb 25, 7:02 am, Tim Chase <python.l...@tim.thechases.com> wrote:
> Python 3 introduced a variable tuple assignment which I
> suspect[*] would work in this context:
>
>    for first, *rest in L: # note the asterisk
>      print first
>      for x in rest:
>        do_stuff(x)
>
> [*] not having py3 on this machine, I can't readily verify this.

Seems fine under 3.1. Cool.

>>> L = ((1,2,3),
(4,),
(5,),
(6,7)
)
>>> for first, *rest in L:
print('first',first,end=' ')
print('rest',rest)

first 1 rest [2, 3]
first 4 rest []
first 5 rest []
first 6 rest [7]