Log in

View Full Version : Dictionary from two lists...


Tobiah
April 9th 07, 10:56 PM
I know there must be a concise way, and elegant way,
to do this:

>>> foo = ['cat', 'dog', 'mouse']
>>> bar = ['bat', 'hog', 'house']
>>> cool_trick(foo, bar)
{'cat': 'bat', 'dog': 'hog', 'mouse': 'house'}

Thanks,

Toby

--
Posted via a free Usenet account from http://www.teranews.com

Scott Dorsey
April 10th 07, 01:58 PM
Tobiah > wrote:
>I know there must be a concise way, and elegant way,
>to do this:
>
> >>> foo = ['cat', 'dog', 'mouse']
> >>> bar = ['bat', 'hog', 'house']
> >>> cool_trick(foo, bar)
>{'cat': 'bat', 'dog': 'hog', 'mouse': 'house'}
>
>Thanks,

In what language? Doing this in an awk script isn't that hard.
--scott

--
"C'est un Nagra. C'est suisse, et tres, tres precis."

Tobiah
April 10th 07, 07:59 PM
Scott Dorsey wrote:
> Tobiah > wrote:
>> I know there must be a concise way, and elegant way,
>> to do this:
>>
>>>>> foo = ['cat', 'dog', 'mouse']
>>>>> bar = ['bat', 'hog', 'house']
>>>>> cool_trick(foo, bar)
>> {'cat': 'bat', 'dog': 'hog', 'mouse': 'house'}
>>
>> Thanks,
>
> In what language? Doing this in an awk script isn't that hard.
> --scott
>

Sorry, this was meant for comp.lang.python. I
found that I could do:

dict(zip(foo, bar))

--
Posted via a free Usenet account from http://www.teranews.com

Willie K. Yee, MD[_3_]
April 11th 07, 12:16 AM
On Tue, 10 Apr 2007 11:59:38 -0700, Tobiah > wrote:

>Scott Dorsey wrote:
>> Tobiah > wrote:
>>> I know there must be a concise way, and elegant way,
>>> to do this:
>>>
>>>>>> foo = ['cat', 'dog', 'mouse']
>>>>>> bar = ['bat', 'hog', 'house']
>>>>>> cool_trick(foo, bar)
>>> {'cat': 'bat', 'dog': 'hog', 'mouse': 'house'}
>>>
>>> Thanks,
>>
>> In what language? Doing this in an awk script isn't that hard.
>> --scott
>>
>
>Sorry, this was meant for comp.lang.python. I
>found that I could do:
>
>dict(zip(foo, bar))
>
>--
>Posted via a free Usenet account from http://www.teranews.com
>

Kee-rist, Scott, is there any field you DON'T know?


This dude never ceases to amase me.

Mike
April 12th 07, 12:38 AM
On Apr 10, 1:59 pm, Tobiah > wrote:
> Scott Dorsey wrote:
> > Tobiah > wrote:
> >> I know there must be a concise way, and elegant way,
> >> to do this:
>
> >>>>> foo = ['cat', 'dog', 'mouse']
> >>>>> bar = ['bat', 'hog', 'house']
> >>>>> cool_trick(foo, bar)
> >> {'cat': 'bat', 'dog': 'hog', 'mouse': 'house'}
>
> >> Thanks,
>
> > In what language? Doing this in an awk script isn't that hard.
> > --scott
>
> Sorry, this was meant for comp.lang.python. I
> found that I could do:
>
> dict(zip(foo, bar))
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com

That will work nicely, here's an example from a project of mine where
I create a list of dictionary objects from a single list.

def getTemplateAssociations(UID):
descriptions = []
i = 0
temps = context.getTemplatesForDieID(die=UID).dictionaries ()
tempMethod = context.getTemplateID
result = []
for y in temps:
descriptions.append('TemplateID')
descriptions.append(temps[i].get('TemplateID'))
descriptions.append('Description')

descriptions.append(tempMethod(templateID=temps[i].get('TemplateID'))
[0][0])
pair = dict(zip(descriptions[:-1:2], descriptions[1::2]))
#convert list to dict
result.append(pair) #make a list of dict objects
i += 1

return result

Since you are already on that path I figured sooner or later you'll
need to make lists of dictionaries. It's a Pythonic necessity.

Mike