Conjunction List

George Sakkis george.sakkis at gmail.com
Sun Jun 1 18:42:01 CEST 2008


On May 31, 8:01 pm, ccy56... at gmail.com wrote:
> http://codepad.org/MV3k10AU
>
> I want to write like next one.
>
> def conjunction(number=a[1],name=b[1],size=c[1]):
>   flag = a[0]==b[0]==c[0]
>   if flag:
>     for e in zip(number,name,size):
>       print e
>
> conjunction(a,b,c)
>
> -----------------------------------------------------------------------------------------------------
> function args receive sequence element:
>
> def somefunc(name=elm1[1], size=elm2[1], color=elm3[1]):
>   for e in zip(name, size, color):
>     print e,
>
> conjunction(a,b,c)
> -----------------------------------------------------------------------------------------------------
>
> not like two case:
> -----------------------------------------------------------------------------------------------------
>
> def somefunc(elm1, elm2, elm3):
>   name = elm1[1]; size = elm1[1]; color = elm1[1] # best solution?
>   for e in zip(name, size, color):
>     print e,
>
> conjunction(a,b,c)
> -----------------------------------------------------------------------------------------------------
>
> def somefunc(elm1, elm2, elm3, **attr):
>   for e in zip(attr['name'], attr['size'], attr['color']):
>     print e,
>
> conjunction(a,b,c, name=a[1], size=b[1], color=c[1]) # many args...
> -----------------------------------------------------------------------------------------------------
>
> What's a good approach to get the conjunction nest-list-data?

The one you comment with "best solution?" is ok for this example. If
you dislike the repetitive part of setting the flag and getting the
second element of each argument, or especially if you want to
generalize it to more than three arguments, here's one way to do it:

    def conjunction(*args):
        if not args: return
        first = args[0][0]
        if all(arg[0]==first for arg in args):
            for e in zip(*(arg[1] for arg in args)):
                print e

    >>> conjuction(a,b,c)
    ('0', 'one', '0%')
    ('1', 'two', '50%')
    ('2', 'three', '100%')

HTH,
George



More information about the Python-list mailing list