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


Groups > comp.lang.python > #84973

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'element': 0.07; 'remaining': 0.07; 'subject:two': 0.07; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.12; 'language.': 0.14; 'enough.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:[ 40': 0.16; 'skip:{ 40': 0.16; 'subject:based': 0.16; 'subject:key': 0.16; 'index': 0.16; 'sat,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'code,': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'code': 0.31; 'subject:per': 0.31; 'subject:from': 0.34; 'could': 0.34; 'equal': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'adjust': 0.36; 'right?': 0.36; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'our': 0.64; 'charset:windows-1252': 0.65; 'line,': 0.68; '2015': 0.84; 'convenience,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Create dictionary based of x items per key from two lists
Date Sun, 01 Feb 2015 03:06:21 +0000
References <0dddee06-233b-436a-be48-3c16e62c1718@googlegroups.com> <CAPTjJmoJmr6PUwGEtpOZ=i2Kff=AMEuuK2kvzuT23fefJg1j8g@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-92-24-222-48.ppp.as43234.net
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0
In-Reply-To <CAPTjJmoJmr6PUwGEtpOZ=i2Kff=AMEuuK2kvzuT23fefJg1j8g@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18345.1422759990.18130.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1422759990 news.xs4all.nl 2884 [2001:888:2000:d::a6]:36190
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:84973

Show key headers only | View raw


On 31/01/2015 02:38, Chris Angelico wrote:
> On Sat, Jan 31, 2015 at 1:27 PM,  <rajanbond@gmail.com> wrote:
>> 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
>> }
>>
>> So last item from l2 is key for remaining items from l1
>
> So the Nth element of l1 will always be paired with the
> (N/numL1PerL2)th element of l2 (with the check at the end)? Seems easy
> enough.
>
> dups = len(l1)/len(l2)
> l2.append(l2[-1])
> result = {x:l2[i/dups] for i,x in enumerate(l1)}
>
> This mutates l2 for convenience, but you could also adjust the index
> to take care of the excess. As a one-liner:
>
> result = {x:l2[min(i/(len(l1)/len(l2)),len(l2)-1)] for i,x in enumerate(l1)}
>
> But the one-liner is not better code :)
>
> ChrisA
>

The one-liner might not be better code, but it must be better speed wise 
precisely because it's on one line, right? :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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