Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Is there any nice way to unpack a list of unknown size??

Reply
Thread Tools

Re: Is there any nice way to unpack a list of unknown size??

 
 
Gary Herron
Guest
Posts: n/a
 
      09-14-2008
srinivasan srinivas wrote:
> I want to do something like below:
>
> 1. first, second, third, *rest = foo
>


Python 3.0 has exactly this feature. No current Python 2.x version has it.

Gary Herron

> 2. for (a,b,c,*rest) in list_of_lists:
>
> Please suggest.
>
> Thanks,
> Srini
>
>
> Bring your gang together. Do your thing. Find your favourite Yahoo! group at http://in.promos.yahoo.com/groups/
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      09-14-2008
On Sep 14, 4:08*pm, Gary Herron <(E-Mail Removed)> wrote:
> srinivasan srinivas wrote:
> > I want to do something like below:

>
> > 1. first, second, third, *rest = foo

>
> Python 3.0 has exactly this feature. *No current Python 2.x version has it.
>
> Gary Herron
>
> > *2. for (a,b,c,*rest) in list_of_lists:

>
> > Please suggest.

>
> > Thanks,
> > Srini

>
> > * * * Bring your gang together. Do your thing. Find your favourite Yahoo! group athttp://in.promos.yahoo.com/groups/
> > --
> >http://mail.python.org/mailman/listinfo/python-list


In python >= 2.4, you can define a function like this:

def truncate(iterable, n=1):
iterator = iter(iterable)
for i in iterator:
if n == 0:
yield iterator
return
yield i
n -= 1

>>> a, b, c, tail = truncate([1,2,3,4,5,6], 3)
>>> a

1
>>> b

2
>>> c

3
>>> tail

<listiterator object at 0x78990>
>>> list(tail)

[5, 6]

--
Arnaud


 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      09-14-2008
>> I want to do something like below:
>> 1. first, second, third, *rest = foo

>
> Python 3.0 has exactly this feature. No current Python 2.x version has it.


I asked something similar[1] on c.l.p a while back and Diez
Roggisch gave this nice workaround/hack[2]

It's a bit ugly in the implementation (sniffing the stack), but
elegant in the use, doing exactly what I needed.

It doesn't accommodate the "rest" portion that you reference, but
allows for arbitrary unpacking into a fixed-length tuple.

-tkc


[1]
http://mail.python.org/pipermail/pyt...ay/381386.html

[2]
http://mail.python.org/pipermail/pyt...ay/381399.html




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any way to decode String using unknown codec? howmuchistoday@gmail.com Python 5 06-28-2012 09:27 PM
Is there any nice way to unpack a list of unknown size?? srinivasan srinivas Python 0 09-14-2008 02:57 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Need better way to unpack a list Nx Python 3 08-17-2005 01:27 PM
is there a nice way to read consequtive integers from a character sting DeadInPlastic C++ 1 06-26-2003 01:09 PM



Advertisments