Is there a better algorithm?

alex goretoy aleksandr.goretoy at gmail.com
Sat Jan 3 00:18:28 EST 2009


I personally like this solution the best. Thanks mr.

tuples = [(1, 2), (3, 4, 5), (6, 7)]

def triple_or_pair(seq):
   u = None
   try:
       k, u, v = seq
   except ValueError:
       k, v = seq
   return k, u, v

for k, u, v in [ triple_or_pair(seq) for seq in tuples ]:
   print k, u, v

On Sat, Jan 3, 2009 at 4:55 AM, Kottiyath <n.kottiyath at gmail.com> wrote:

> On Jan 3, 2:38 am, mr <mario.rugg... at gmail.com> wrote:
> > As has been noted, the best is to fix the input to be regular-3-
> > tuples. For the fun of it, here's another variation of a solution:
> >
> > tuples = [(1, 2), (3, 4, 5), (6, 7)]
> >
> > def triple_or_pair(seq):
> >     u = None
> >     try:
> >         k, u, v = seq
> >     except ValueError:
> >         k, v = seq
> >     return k, u, v
> >
> > for k, u, v in [ triple_or_pair(seq) for seq in tuples ]:
> >     print k, u, v
>
> It is a code to post some data to HTML server.
> Even though usually the POST values are of type(name, value), if file
> transfer is involved, then POST values change to (name, filename,
> value).
> My view was that since filename is a rare occurance and doesnt make
> sense in a usual POST, I had not kept it as a full 3 tuple.
> Since so many programmers (that too much more capable than me) are
> suggesting that it is code smell, I am reviewing my decision.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090103/c5d5ef55/attachment-0001.html>


More information about the Python-list mailing list