[Tutor] all right students, what do we learn

Dave Angel davea at davea.name
Sun Nov 2 10:17:54 CET 2014


On 11/02/2014 01:43 AM, Clayton Kirkwood wrote:
> To prove that a little knowledge is a little stupid or something to that
> effect:
>
> #    for key in key_list:
>
> #        print(key)
>
> #        if key not in key_list0:
>
> #            print("Error:", key, "not available, start again")
>
> #            get_new_list = True
>
> #            break
>
> #    else: get_new_list = False
>
>
>
> Assume key_list0 = [('a', 'Ask'), ('y', 'Dividend Yield')]
>
>
>
> The above worked when key_list was 'a y' or 'hhh'
>

For what definition of "worked"?

You have no comments as to what the loop is supposed to produce, nor 
what its inputs are supposed to look like.  Neither of the values for 
key_list are lists, so what's up with that?  Further, it appears you 
want to compare  one-letter strings to one element of a tuple.

>
>
> Well, if a little cool was good why not use all of my new found knowledge of
> comprehensions.

Why?  There are ways to simplify it, but list comprehension isn't one of 
them.  I'd be looking into sets.

Something like (untested):
     get_new_list = not set(key_list).issubset(set(key_list0))

>
> So I head down the path of all sorts of variations of the following. And I
> mean all sorts. Instead of [reenter, key_columns] I used a single list.
> Instead of the exec('reenter = True') I used a simple reenter=True (and an

This has become total nonsense to me.  There may be something you're 
aiming at, but I think it's a long way from April Fool.

Perhaps you're trying for a Python obfuscation contest.  But that's a C 
tradition, not Python.  In Python, readability is preferred.

> eval and compile which I don't totally understand yet).

"exec" and "eval" ?  Run for the woods.

If you like writing obscure code, then you can do it with a list 
comprehension.  Just wrap the comprehension in an all() or any() call. 
Don't try to set external variables in an inner scope.

Something like (untested):
   get_new_list = any( [ True for item in key_list if item not in 
key_list0] )

This has the disadvantage that it won't stop on the first missing key, 
but will try them all regardless.

In any case, you should probably make two functions, one with the 
reference implementation, and one with the experiment.  And your test 
loop should call both of them, compare the results, and indicate when 
there's a discrepancy.



-- 
DaveA


More information about the Tutor mailing list