{ '0':'c->c->a' ,'1':'a->b->a' .........}

Discussion in 'Python' started by chris, Nov 7, 2010.

  1. chris

    chris Guest

    Hi,

    have anybody a hint , how i get a dict from non unique id's and their
    different related values.

    Thanks for advance
    Chris

    ###random data #
    a=range(10)*3
    def seqelem():
    i=random.randint(0,2)
    elem=['a','b','c']
    return elem

    s=[seqelem() for t in range(30)]
    print zip(a,s)

    ## favored result:
    { '0':'c->c->a' ,'1':'a->b->a' .........}
    chris, Nov 7, 2010
    #1
    1. Advertising

  2. chris

    John Ladasky Guest

    Hi Chris,

    I may have time to look at the rest of your code later. For now I
    just want to comment on one line:

    On Nov 7, 12:24 pm, chris <> wrote:

    >     elem=['a','b','c']


    The string type, just like the list type, is a sequence type. So
    strings have all the standard sequence methods. You could just write:

    elem = "abc"
    John Ladasky, Nov 7, 2010
    #2
    1. Advertising

  3. chris <> writes:

    > Hi,
    >
    > have anybody a hint , how i get a dict from non unique id's and their
    > different related values.
    >
    > Thanks for advance
    > Chris
    >
    > ###random data #
    > a=range(10)*3
    > def seqelem():
    > i=random.randint(0,2)
    > elem=['a','b','c']
    > return elem
    >
    > s=[seqelem() for t in range(30)]
    > print zip(a,s)
    >
    > ## favored result:
    > { '0':'c->c->a' ,'1':'a->b->a' .........}


    Here's one way:

    >>> import random
    >>>
    >>> a=range(10)*3
    >>> def seqelem():

    .... i=random.randint(0,2)
    .... elem=['a','b','c']
    .... return elem
    ....
    >>> s=[seqelem() for t in range(30)]
    >>> z = zip(a, s)
    >>> print z

    [(0, 'b'), (1, 'a'), (2, 'b'), (3, 'a'), (4, 'b'), (5, 'c'), (6, 'b'), (7, 'c'), (8, 'b'), (9, 'b'), (0, 'a'), (1, 'b'), (2, 'b'), (3, 'c'), (4, 'c'), (5, 'b'), (6, 'c'), (7, 'a'), (8, 'a'), (9, 'c'), (0, 'b'), (1, 'c'), (2, 'b'), (3, 'a'), (4, 'c'), (5, 'a'), (6, 'c'), (7, 'b'), (8, 'c'), (9, 'c')]
    >>>
    >>> from itertools import groupby
    >>> from operator import itemgetter
    >>>
    >>> z.sort(key=itemgetter(0))
    >>> print dict((k, '->'.join(map(itemgetter(1), it)))

    .... for k, it in groupby(z, itemgetter(0)))
    {0: 'b->a->b', 1: 'a->b->c', 2: 'b->b->b', 3: 'a->c->a', 4: 'b->c->c', 5: 'c->b->a', 6: 'b->c->c', 7: 'c->a->b', 8: 'b->a->c', 9: 'b->c->c'}

    HTH

    --
    Arnaud
    Arnaud Delobelle, Nov 7, 2010
    #3
  4. chris

    Peter Otten Guest

    chris wrote:
    > have anybody a hint , how i get a dict from non unique id's and their
    > different related values.
    >
    > Thanks for advance
    > Chris
    >
    > ###random data #
    > a=range(10)*3
    > def seqelem():
    > i=random.randint(0,2)
    > elem=['a','b','c']
    > return elem
    >
    > s=[seqelem() for t in range(30)]
    > print zip(a,s)
    >
    > ## favored result:
    > { '0':'c->c->a' ,'1':'a->b->a' .........}


    >>> import random
    >>> from collections import defaultdict
    >>> a = range(10)*3
    >>> s = [random.choice("abc") for _ in a]
    >>> d = defaultdict(list)
    >>> for k, v in zip(a, s):

    .... d[k].append(v)
    ....
    >>> d

    defaultdict(<type 'list'>, {0: ['b', 'a', 'a'], 1: ['c', 'a', 'c'], 2: ['c',
    'c', 'c'], 3: ['c', 'a', 'a'], 4: ['b', 'c', 'a'], 5: ['b', 'c', 'c'], 6:
    ['c', 'a', 'b'], 7: ['b', 'b', 'a'], 8: ['a', 'c', 'c'], 9: ['b', 'a',
    'b']})
    >>> dict((k, "->".join(v)) for k, v in d.iteritems())

    {0: 'b->a->a', 1: 'c->a->c', 2: 'c->c->c', 3: 'c->a->a', 4: 'b->c->a', 5:
    'b->c->c', 6: 'c->a->b', 7: 'b->b->a', 8: 'a->c->c', 9: 'b->a->b'}

    Peter
    Peter Otten, Nov 7, 2010
    #4
  5. chris

    Sells, Fred Guest

    Since your keys are not unique, I would think that you would want a list
    of values for the object corresponding to each key. Something like

    Mydict = {}

    Mydict.setdefault(mykey, []).append(avalue)

    -----Original Message-----
    From: python-list-bounces+frsells=
    [mailto:python-list-bounces+frsells=] On
    Behalf Of Peter Otten
    Sent: Sunday, November 07, 2010 4:01 PM
    To:
    Subject: Re: { '0':'c->c->a' ,'1':'a->b->a' .........}

    chris wrote:
    > have anybody a hint , how i get a dict from non unique id's and their
    > different related values.
    >
    > Thanks for advance
    > Chris
    >
    > ###random data #
    > a=range(10)*3
    > def seqelem():
    > i=random.randint(0,2)
    > elem=['a','b','c']
    > return elem
    >
    > s=[seqelem() for t in range(30)]
    > print zip(a,s)
    >
    > ## favored result:
    > { '0':'c->c->a' ,'1':'a->b->a' .........}


    >>> import random
    >>> from collections import defaultdict
    >>> a = range(10)*3
    >>> s = [random.choice("abc") for _ in a]
    >>> d = defaultdict(list)
    >>> for k, v in zip(a, s):

    .... d[k].append(v)
    ....
    >>> d

    defaultdict(<type 'list'>, {0: ['b', 'a', 'a'], 1: ['c', 'a', 'c'], 2:
    ['c',
    'c', 'c'], 3: ['c', 'a', 'a'], 4: ['b', 'c', 'a'], 5: ['b', 'c', 'c'],
    6:
    ['c', 'a', 'b'], 7: ['b', 'b', 'a'], 8: ['a', 'c', 'c'], 9: ['b', 'a',
    'b']})
    >>> dict((k, "->".join(v)) for k, v in d.iteritems())

    {0: 'b->a->a', 1: 'c->a->c', 2: 'c->c->c', 3: 'c->a->a', 4: 'b->c->a',
    5:
    'b->c->c', 6: 'c->a->b', 7: 'b->b->a', 8: 'a->c->c', 9: 'b->a->b'}

    Peter
    --
    http://mail.python.org/mailman/listinfo/python-list
    Sells, Fred, Nov 9, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page