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

C

chris

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' .........}
 
J

John Ladasky

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:

    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"
 
A

Arnaud Delobelle

chris said:
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:
.... 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
 
P

Peter Otten

chris said:
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)
....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
 
S

Sells, Fred

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: [email protected]
[mailto:p[email protected]] On
Behalf Of Peter Otten
Sent: Sunday, November 07, 2010 4:01 PM
To: (e-mail address removed)
Subject: Re: { '0':'c->c->a' ,'1':'a->b->a' .........}
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)
....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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top