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


Groups > comp.lang.python > #5493

Re: Converting a set into list

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.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.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'python': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'solution,': 0.09; 'wrote:': 0.14; 'contrary,': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'runs,': 0.16; 'subject:Converting': 0.16; 'subject:set': 0.16; 'transforming': 0.16; 'subject:list': 0.22; 'loop': 0.22; 'equivalent': 0.26; 'daniel': 0.29; 'list': 0.30; 'seem': 0.30; 'from:addr:web.de': 0.31; 'operation.': 0.31; 'to:addr:python- list': 0.32; 'another': 0.32; 'header:X-Complaints-To:1': 0.34; 'skip:" 10': 0.34; 'less': 0.38; 'but': 0.38; 'received:org': 0.38; 'set': 0.39; 'to:addr:python.org': 0.39; 'header:Mime- Version:1': 0.39; 'header:Received:5': 0.40; 'might': 0.40; 'best': 0.60; 'back': 0.61; 'conversion': 0.64; '100': 0.70; 'concerns': 0.80; '1.99': 0.84; 'marginally': 0.84; 'speed,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Converting a set into list
Date Mon, 16 May 2011 08:34:44 +0200
Organization None
References <iqlgj4$iet$1@speranza.aioe.org> <87iptdid68.fsf@benfinney.id.au> <iqm4tk$3sl$1@speranza.aioe.org> <871v00j2bh.fsf@benfinney.id.au> <iqncnv01u6u@news5.newsguy.com> <34fc571c-f382-405d-94b1-0a673da5f46b@t16g2000vbi.googlegroups.com> <iqot4d$cd6$1@speranza.aioe.org> <iqpd07$3gs$1@r03.glglgl.eu> <BANLkTimCpRso07q7FKgt4iCz3W8t6w748Q@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084908d.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1629.1305527630.9059.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 82.94.164.166
X-Trace 1305527630 news.xs4all.nl 81474 [::ffff:82.94.164.166]:60771
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5493

Show key headers only | View raw


Daniel Kluev wrote:

>> Both solutions seem to be equivalent in that concerns the number of
>> needed loop runs, but this two-step operation might require one less loop
>> over list1. The set&set solution, in contrary, might require one loop
>> while transforming to a set and another one for the & operation.
> 
> python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)"
> "l3 = list(set(l1) & set(l2))"
> 100 loops, best of 3: 2.19 msec per loop
> 
> python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)"
> "s=set(l2); l3 = [i for i in l1 if i in s]"
> 100 loops, best of 3: 2.45 msec per loop
> 
> python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)"
> "l3 = list(set(l1) & set(l2))"
> 10 loops, best of 3: 28 msec per loop
> 
> python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)"
> "s=set(l2); l3 = [i for i in l1 if i in s]"
> 10 loops, best of 3: 28.1 msec per loop
> 
> So even with conversion back into list set&set is still marginally faster.

If you are looking for speed, consider

s = set(l1)
s.intersection_update(l2)
l3 = list(s) 

$ python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)" 
"list(set(l1) & set(l2))"
100 loops, best of 3: 4 msec per loop

$ python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)" "s = 
set(l1); s.intersection_update(l2); list(s)"
100 loops, best of 3: 1.99 msec per loop

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


Thread

Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-14 17:02 +0800
  Re: Converting a set into list Peter Otten <__peter__@web.de> - 2011-05-14 11:33 +0200
    Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-14 22:14 +0800
      Re: Converting a set into list Chris Angelico <rosuav@gmail.com> - 2011-05-15 04:22 +1000
  Re: Converting a set into list Ben Finney <ben+python@benfinney.id.au> - 2011-05-15 00:12 +1000
    Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-14 22:51 +0800
      Re: Converting a set into list Ben Finney <ben+python@benfinney.id.au> - 2011-05-15 09:21 +1000
        Re: Converting a set into list Chris Torek <nospam@torek.net> - 2011-05-15 02:11 +0000
          Re: Converting a set into list SigmundV <sigmundv@gmail.com> - 2011-05-15 04:18 -0700
            Re: Converting a set into list SigmundV <sigmundv@gmail.com> - 2011-05-15 04:23 -0700
            Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-15 23:56 +0800
              Re: Converting a set into list Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-05-15 22:27 +0200
                Re: Converting a set into list Daniel Kluev <dan.kluev@gmail.com> - 2011-05-16 13:37 +1100
                Re: Converting a set into list Peter Otten <__peter__@web.de> - 2011-05-16 08:34 +0200
                Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-16 22:23 +0800
                Re: Converting a set into list Ben Finney <ben+python@benfinney.id.au> - 2011-05-17 10:33 +1000
            Re: Converting a set into list Roy Smith <roy@panix.com> - 2011-05-15 13:07 -0400
          Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-16 00:05 +0800
            Re: Converting a set into list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-15 16:28 +0000
              Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-16 00:35 +0800
          Re: Converting a set into list Duncan Booth <duncan.booth@invalid.invalid> - 2011-05-16 10:24 +0000
            Re: Converting a set into list Chris Torek <nospam@torek.net> - 2011-05-17 01:07 +0000

csiph-web