Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85018

Re: Create dictionary based of x items per key from two lists

References <0dddee06-233b-436a-be48-3c16e62c1718@googlegroups.com> <CANy1k1iTMpiUdXaFRqfvt_P=jLk08yVohKxGtY8rXVcwUxiCKw@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-02-01 11:51 -0700
Subject Re: Create dictionary based of x items per key from two lists
Newsgroups comp.lang.python
Message-ID <mailman.18370.1422816759.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jan 31, 2015 at 7:38 PM, Jason Friedman <jsf80238@gmail.com> wrote:
>> I have two lists
>>
>> l1 =  ["a","b","c","d","e","f","g","h","i","j"]
>> l2 = ["aR","bR","cR"]
>>
>> l2 will always be smaller or equal to l1
>>
>> numL1PerL2 = len(l1)/len(l2)
>>
>> I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
>>
>> So
>>
>> {
>> a:aR,
>> b:aR,
>> c:aR,
>> d:bR,
>> e:bR,
>> f:bR,
>> g:cR,
>> h:cR,
>> i:cR,
>> j:cR
>> }
>
> Another possibility is:
> import itertools
> my_dict = {x:y for x,y in zip(list1, itertools.cycle(list2))}

That results in a different mapping than the one specified by the OP,
though. An alternate itertools approach could be:

dict(zip_longest(l1, chain.from_iterable(
    map(partial(repeat, times=len(l1) // len(l2)), l2)), fillvalue=l2[-1]))

although this requires the inputs to be sequences, not arbitrary iterables.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Create dictionary based of x items per key from two lists rajanbond@gmail.com - 2015-01-30 18:27 -0800
  Re: Create dictionary based of x items per key from two lists Chris Angelico <rosuav@gmail.com> - 2015-01-31 13:38 +1100
  Re: Create dictionary based of x items per key from two lists Jason Friedman <jsf80238@gmail.com> - 2015-01-31 19:38 -0700
    Re: Create dictionary based of x items per key from two lists rajanbond@gmail.com - 2015-02-02 10:48 -0800
    Re: Create dictionary based of x items per key from two lists rajanbond@gmail.com - 2015-02-02 11:00 -0800
  Re: Create dictionary based of x items per key from two lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-01 03:06 +0000
  Re: Create dictionary based of x items per key from two lists Chris Angelico <rosuav@gmail.com> - 2015-02-01 14:22 +1100
    Re: Create dictionary based of x items per key from two lists Grant Edwards <invalid@invalid.invalid> - 2015-02-01 18:14 +0000
      Re: Create dictionary based of x items per key from two lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-01 18:49 +0000
      Re: Create dictionary based of x items per key from two lists mm0fmf <none@mailinator.com> - 2015-02-01 18:52 +0000
      Re: Create dictionary based of x items per key from two lists Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-01 12:36 -0700
        Re: Create dictionary based of x items per key from two lists wxjmfauth@gmail.com - 2015-02-02 23:13 -0800
      Re: Create dictionary based of x items per key from two lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-01 20:28 +0000
      Re: Create dictionary based of x items per key from two lists Chris Angelico <rosuav@gmail.com> - 2015-02-02 07:33 +1100
  Re: Create dictionary based of x items per key from two lists Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-01 11:51 -0700

csiph-web