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


Groups > comp.lang.python > #5444

Re: Converting a set into list

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: Converting a set into list
Date 2011-05-15 22:27 +0200
Organization A newly installed InterNetNews server
Message-ID <iqpd07$3gs$1@r03.glglgl.eu> (permalink)
References (2 earlier) <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>

Show all headers | View raw


Am 15.05.2011 17:56 schrieb TheSaint:
> SigmundV wrote:
>
>> I think the OP wants to find the intersection of two lists.
>> list(set(list1)&  set(list2)) is indeed one way to achieve this. [i
>> for i in list1 if i in list2] is another one
>
> Exactly. I was confused on that I wasn't able to have a list in return.
> The set intersection is the smartest result better than a "for" loop or a
> comprehension list.

I'm not sure about if it is really the smartest way.

s=set(list2);  [i for i in list1 if i in s]
is in the same order of magnitude as the set operation. 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.


> Infact the operatin loops are compiled into python, therfore they are the
> fastest.

Which loops do you mean here?


Thomas

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