{ '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. Advertisements

  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:



    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. Advertisements



  3. Here's one way:
    .... i=random.randint(0,2)
    .... elem=['a','b','c']
    .... return elem
    ....
    .... 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 Delobelle, Nov 7, 2010
    #3
  4. chris

    Peter Otten Guest


    .... d[k].append(v)
    ....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']}){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' .........}

    .... d[k].append(v)
    ....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']}){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
     
    Sells, Fred, Nov 9, 2010
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.