Velocity Reviews > Is there a better algorithm?

Is there a better algorithm?

Bruno Desthuilliers
Guest
Posts: n/a

 01-02-2009
Kottiyath a écrit :
> I have the following list of tuples:
> L = [(1, 2), (3, 4, 5), (6, 7)]
>
> I want to loop through the list and extract the values.
> The only algorithm I could think of is:
>>>> for i in l:

> ... u = None
> ... try:
> ... (k, v) = i
> ... except ValueError:
> ... (k, u, v) = i
> ... print k, u, v
> ---------
> 1 None 2
> 3 4 5
> 6 None 7
> -------------
> But, this algorithm doesnt look very beautiful - like say -> for k, u,
> v in L:
> Can anyone suggest a better algorithm to get the values?

complement = lambda t: (t[0], None, t[1]) if len(t) == 2 else t
for k, u, v in map(complement, L):
print k, u, v

Kottiyath
Guest
Posts: n/a

 01-02-2009
I have the following list of tuples:
L = [(1, 2), (3, 4, 5), (6, 7)]

I want to loop through the list and extract the values.
The only algorithm I could think of is:
>>> for i in l:

.... u = None
.... try:
.... (k, v) = i
.... except ValueError:
.... (k, u, v) = i
.... print k, u, v
---------
1 None 2
3 4 5
6 None 7
-------------
But, this algorithm doesnt look very beautiful - like say -> for k, u,
v in L:
Can anyone suggest a better algorithm to get the values?

Fuzzyman
Guest
Posts: n/a

 01-02-2009
On Jan 2, 6:11*pm, Kottiyath <(E-Mail Removed)> wrote:
> I have the following list of tuples:
> L = [(1, 2), (3, 4, 5), (6, 7)]
>
> I want to loop through the list and extract the values.
> The only algorithm I could think of is:>>> for i in l:
>
> ... *u = None
> ... *try:
> ... * (k, v) = i
> ... *except ValueError:
> ... * (k, u, v) = i
> ... *print k, u, v
> ---------
> 1 None 2
> 3 4 5
> 6 None 7
> -------------
> But, this algorithm doesnt look very beautiful - like say -> for k, u,
> v in L:
> Can anyone suggest a better algorithm to get the values?

for i in l:
u = None
if len(i) == 2:
k, v = i
else:
k, u, v = i

Best I could come up with.

Alternatively:

def mangle(i):
if len(i) == 3:
return i
k, v = i
return k, None, v

for i in l:
k, u, v = mangle(i)

I'm sure there is a clever one liner using the Python 2.5 ternary
expression syntax. On the other hand I'm not sure it would be very
readable, so a straightforward (if less clever) solution is probably
better.

Michael
--
http://www.ironpythoninaction.com/

Steve Holden
Guest
Posts: n/a

 01-02-2009
Kottiyath wrote:
> I have the following list of tuples:
> L = [(1, 2), (3, 4, 5), (6, 7)]
>
> I want to loop through the list and extract the values.
> The only algorithm I could think of is:
>>>> for i in l:

> ... u = None
> ... try:
> ... (k, v) = i
> ... except ValueError:
> ... (k, u, v) = i
> ... print k, u, v
> ---------
> 1 None 2
> 3 4 5
> 6 None 7
> -------------
> But, this algorithm doesnt look very beautiful - like say -> for k, u,
> v in L:
> Can anyone suggest a better algorithm to get the values?

L = [(1, 2), (3, 4, 5), (6, 7)]
for i in L:
if len(i) == 2:
k, v, u = i + (None, )
else:
k, u, v = i
print k, u, v

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Markus Brueckner
Guest
Posts: n/a

 01-02-2009
Hi,

Fuzzyman wrote:

> I'm sure there is a clever one liner using the Python 2.5 ternary
> expression syntax. On the other hand I'm not sure it would be very
> readable, so a straightforward (if less clever) solution is probably
> better.

that would be something like this (using a generator)

L = [(1, 2), (3, 4, 5), (6, 7)]
g = ( ((e[0],None,e[1]) if len(e)==2 else (e[0],e[1],e[2])) for e in L)

for elem in g:
print elem

So long,
Markus
--
"Hacking is like having sex: you get in, you get out and you hope that you
did not leave anything which can be traced back to you" - Unbekannt

Francesco Bochicchio
Guest
Posts: n/a

 01-02-2009
Kottiyath ha scritto:
> I have the following list of tuples:
> L = [(1, 2), (3, 4, 5), (6, 7)]
>
> I want to loop through the list and extract the values.
> The only algorithm I could think of is:
>>>> for i in l:

> ... u = None
> ... try:
> ... (k, v) = i
> ... except ValueError:
> ... (k, u, v) = i
> ... print k, u, v
> ---------
> 1 None 2
> 3 4 5
> 6 None 7
> -------------
> But, this algorithm doesnt look very beautiful - like say -> for k, u,
> v in L:
> Can anyone suggest a better algorithm to get the values?

One way to avoid explicit checks on tuple size (but making the code a
bit less clear and probably slower):

for i in l:
k, v, u = (i[:3]+(None,))[:3]
...

Ciao
-----
FB

Andreas Waldenburger
Guest
Posts: n/a

 01-02-2009
On Fri, 02 Jan 2009 19:55:43 +0100 Markus Brueckner <(E-Mail Removed)>
wrote:

> g = ( ((e[0],None,e[1]) if len(e)==2 else (e[0],e[1],e[2])) for e in
> L)

If this isn't proof of Python's versatility, I don't know what is. In
one line it can mimic both Lisp and Perl. Sweet.

/W

--
My real email address is constructed by swapping the domain with the
recipient (local part).

J Kenneth King
Guest
Posts: n/a

 01-02-2009
Kottiyath <(E-Mail Removed)> writes:

> I have the following list of tuples:
> L = [(1, 2), (3, 4, 5), (6, 7)]
>
> I want to loop through the list and extract the values.
> The only algorithm I could think of is:
>>>> for i in l:

> ... u = None
> ... try:
> ... (k, v) = i
> ... except ValueError:
> ... (k, u, v) = i
> ... print k, u, v
> ---------
> 1 None 2
> 3 4 5
> 6 None 7
> -------------
> But, this algorithm doesnt look very beautiful - like say -> for k, u,
> v in L:
> Can anyone suggest a better algorithm to get the values?

Just a note: this isn't really an algorithm problem.

It's more of a grammar obstruction.

To make your code more simple, it would be nice if the assignment
operator would return, "None," in the case where there are too few
values to unpack from the right-operand of the assignment operator.

Aside from the typical workarounds that first came to mind, I started
wondering whether it was possible to expose the problem and solve it
directly.

Sadly, it appears to be non-trivial (or at least, well hidden from the
unwashed masses).

I'd be really curious if the unpacking machinery were exposed to the
developer. I started poking around the operator and types modules, but
the implementation isn't obvious. What methods are being called on the
operands by the assignment operator in the following statement:

a, b, c = some_tuple

I'm sorry if this line of inquiry is not very pythonic; but one is
curious if this is some python magic happening here. After using the
idiom for years I hadn't really thought about it much until recently.

Paul Rubin
Guest
Posts: n/a

 01-02-2009
Kottiyath <(E-Mail Removed)> writes:
> I have the following list of tuples:
> L = [(1, 2), (3, 4, 5), (6, 7)]
> I want to loop through the list and extract the values.

Others have suggested messy ways to code what you're asking. At another
level, that list format seems like a code smell. You may be better off
organizing the program so that

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

after which unpacking becomes trivial.

bearophileHUGS@lycos.com
Guest
Posts: n/a

 01-02-2009
Fuzzyman:

> for i in l:
> * *u = None
> * *if len(i) == 2:
> * * * k, v = i
> * *else:
> * * * *k, u, v = i

That's the best solution I have seen in this thread so far (but I
suggest to improve indents and use better variable names). In
programming it's generally better to follow the KISS principle.

Bye,
bearophile