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


Groups > comp.lang.python > #7520

Re: What is the most efficient way to compare similar contents in two lists?

From Zachary Dziura <zcdziura@gmail.com>
Newsgroups comp.lang.python
Subject Re: What is the most efficient way to compare similar contents in two lists?
Date 2011-06-13 08:21 -0700
Organization http://groups.google.com
Message-ID <bad3a56e-efba-4ef4-a159-64273cda5a08@gh5g2000vbb.googlegroups.com> (permalink)
References <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> <mailman.169.1307977750.11593.python-list@python.org>

Show all headers | View raw


On Jun 13, 11:09 am, Chris Angelico <ros...@gmail.com> wrote:
> On Tue, Jun 14, 2011 at 12:58 AM, Zachary Dziura <zcdzi...@gmail.com> wrote:
> > if set(source_headers) == set(target_headers):
> >    similar_headers = len(source_headers)
>
> Since you're making sets already, I'd recommend using set operations -
> same_headers is the (length of the) intersection of those two sets,
> and different_headers is the XOR.
>
> # If you need the lists afterwards, use different variable names
> source_headers = set(source_headers)
> target_headers = set(target_headers)
> similar_headers = len(source_headers & target_headers)
> different_headers = len(source_headers ^ target_headers)
>
> Chris Angelico

Wow! That was a lot easier than I thought it would be! I guess I
should have done a little bit more research into such operations.
Thanks a bunch!!

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


Thread

What is the most efficient way to compare similar contents in two lists? Zachary Dziura <zcdziura@gmail.com> - 2011-06-13 07:58 -0700
  Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 01:09 +1000
    Re: What is the most efficient way to compare similar contents in two lists? Zachary Dziura <zcdziura@gmail.com> - 2011-06-13 08:21 -0700
      Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 01:39 +1000
        Re: What is the most efficient way to compare similar contents in two lists? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-13 16:04 +0000
          Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 02:30 +1000
          Re: What is the most efficient way to compare similar contents in two lists? geremy condra <debatem1@gmail.com> - 2011-06-13 10:46 -0700
          Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 03:50 +1000
          Re: What is the most efficient way to compare similar contents in two lists? geremy condra <debatem1@gmail.com> - 2011-06-13 11:20 -0700
          Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 04:24 +1000
          Re: What is the most efficient way to compare similar contents in two lists? Ethan Furman <ethan@stoneleaf.us> - 2011-06-13 12:11 -0700
            Re: What is the most efficient way to compare similar contents in two lists? Zachary Dziura <zcdziura@gmail.com> - 2011-06-13 12:21 -0700
          Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 07:33 +1000
  Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 04:11 +1000
    Re: What is the most efficient way to compare similar contents in two        lists? Chris Torek <nospam@torek.net> - 2011-06-13 18:40 +0000
  Re: What is the most efficient way to compare similar contents in two lists? Chris Angelico <rosuav@gmail.com> - 2011-06-14 04:12 +1000

csiph-web