Google Grupper understøtter ikke længere nye Usenet-opslag eller -abonnementer. Tidligere indhold er fortsat synligt.

Dictionary from a list

6 visninger
Gå til det første ulæste opslag

iu2

ulæst,
19. aug. 2009, 16.31.1719.08.2009
til
Hi all,

I need to create a dictionary out of a list.

Given the list [1, 2, 3, 4, 5, 6]

I need the dictionary: {1:2, 3:4, 5:6}

I'll appreciate your help
Thanks
iu2

Diez B. Roggisch

ulæst,
19. aug. 2009, 16.39.4719.08.2009
til
iu2 schrieb:

> Hi all,
>
> I need to create a dictionary out of a list.
>
> Given the list [1, 2, 3, 4, 5, 6]
>
> I need the dictionary: {1:2, 3:4, 5:6}

dict(zip(l[::2], l[1::2]))

Diez

iu2

ulæst,
19. aug. 2009, 16.52.5419.08.2009
til

Wow, this is cool!

thanks
iu2

Jan Kaliszewski

ulæst,
19. aug. 2009, 20.05.5719.08.2009
til iu2, pytho...@python.org
19-08-2009 o 22:52:54 iu2 <isr...@elbit.co.il> wrote:

> On Aug 19, 11:39 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> iu2 schrieb:
>>
>> > Hi all,
>>
>> > I need to create a dictionary out of a list.
>>
>> > Given the list [1, 2, 3, 4, 5, 6]
>>
>> > I need the dictionary: {1:2, 3:4, 5:6}
>>
>> dict(zip(l[::2], l[1::2]))

Or (for long lists, when memory becomes expensive):

dict(li[i:i+2] for i in xrange(0, len(li), 2))

Or probably better:

from itertools import islice, izip
dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))

Cheers,
*j

--
Jan Kaliszewski (zuo) <z...@chopin.edu.pl>

Jan Kaliszewski

ulæst,
19. aug. 2009, 20.29.3219.08.2009
til Jan Kaliszewski, iu2, pytho...@python.org
20-08-2009 o 02:05:57 Jan Kaliszewski <z...@chopin.edu.pl> wrote:

> Or probably better:
>
> from itertools import islice, izip
> dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))

Or similarly, perhaps more readable:

iterator = iter(li)
dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))

Peter Otten

ulæst,
20. aug. 2009, 02.10.2820.08.2009
til
Jan Kaliszewski wrote:

> 20-08-2009 o 02:05:57 Jan Kaliszewski <z...@chopin.edu.pl> wrote:
>
>> Or probably better:
>>
>> from itertools import islice, izip
>> dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))
>
> Or similarly, perhaps more readable:
>
> iterator = iter(li)
> dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))

I just can't stop posting this one:

>>> from itertools import izip
>>> it = iter([1,2,3,4,5,6])
>>> dict(izip(it, it))


{1: 2, 3: 4, 5: 6}

I really tried, but yours drove me over the edge.

Peter

Steven D'Aprano

ulæst,
20. aug. 2009, 03.05.4120.08.2009
til
On Thu, 20 Aug 2009 08:10:28 +0200, Peter Otten wrote:


> I just can't stop posting this one:
>
>>>> from itertools import izip
>>>> it = iter([1,2,3,4,5,6])
>>>> dict(izip(it, it))
> {1: 2, 3: 4, 5: 6}
>
> I really tried, but yours drove me over the edge.

If you want something to drive you over the edge:


>>> alist = [1, 2, 3, 4, 5, 6]
>>> dict(apply(zip, map(lambda n: map(lambda t: t[1], filter(lambda t:
((not (t[0]%2)) == 1) == n, enumerate(alist))), range(1, -1, -1))))


{1: 2, 3: 4, 5: 6}


Enjoy :)

--
Steven

Peter Otten

ulæst,
20. aug. 2009, 03.45.3720.08.2009
til
Steven D'Aprano wrote:

> On Thu, 20 Aug 2009 08:10:28 +0200, Peter Otten wrote:
>
>
>> I just can't stop posting this one:
>>
>>>>> from itertools import izip
>>>>> it = iter([1,2,3,4,5,6])
>>>>> dict(izip(it, it))
>> {1: 2, 3: 4, 5: 6}
>>
>> I really tried, but yours drove me over the edge.
>
> If you want something to drive you over the edge:

I meant that figuratively...

>>>> alist = [1, 2, 3, 4, 5, 6]
>>>> dict(apply(zip, map(lambda n: map(lambda t: t[1], filter(lambda t:
> ((not (t[0]%2)) == 1) == n, enumerate(alist))), range(1, -1, -1))))
> {1: 2, 3: 4, 5: 6}

...originally.

> Enjoy :)

Not ;)

Tim Chase

ulæst,
20. aug. 2009, 03.47.2520.08.2009
til Peter Otten, pytho...@python.org
Peter Otten wrote:
>>>> it = iter([1,2,3,4,5,6])
>>>> dict(izip(it, it))
> {1: 2, 3: 4, 5: 6}

<devo>Zip(it). Zip(it) good.</devo>

it's-3:00am-and-i-seriously-need-to-sleep'ly yers...

-tkc

iu2

ulæst,
20. aug. 2009, 12.41.1420.08.2009
til

Nice.
(but looks like stepping towards the dark side ... :-)

I also liked this one:


iterator = iter(li)
dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))

which inspired me to do something quite similar:

a=range(1, 11)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> dict([[a.pop(0), a.pop(0)] for i in range(len(a)/2)])
{1: 2, 3: 4, 9: 10, 5: 6, 7: 8}


Thanks

EK

ulæst,
22. aug. 2009, 06.03.2722.08.2009
til

dict(zip(*[iter(l)]*2))

Aahz

ulæst,
23. aug. 2009, 09.11.0523.08.2009
til
In article <baa0d11c-c999-4eee...@b25g2000prb.googlegroups.com>,
EK <eka...@gmail.com> wrote:

>On Aug 20, 2:10=A0pm, Peter Otten <__pete...@web.de> wrote:
>>
>> >>> from itertools import izip
>> >>> it =3D iter([1,2,3,4,5,6])

>> >>> dict(izip(it, it))
>>
>> {1: 2, 3: 4, 5: 6}
>
>dict(zip(*[iter(l)]*2))

No, that's not a good solution. For starters, it's less readable than
Peter's version. More importantly, it has poor memory use because it
needs to construct both the intermediate tuple and the zip() list.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"I support family values -- Addams family values" --www.nancybuttons.com

0 nye opslag