Tuesday, October 15, 2013

Howto figure out whether two lists have any elements in common

While working with FragIt I had to figure out whether two lists had some elements in common (it does not matter which!) and I came up with the following piece of code (with the help of Google and Stack Overflow of course)

It takes advantage of the highly underused Sets available in python on which we can do some neat mathematically sane operations. I guess people just go for Lists and work their magic on that.

6 comments:

  1. And just to get fancy:
    shares_elements=lambda a,b: len(set(a)&set(b))>0

    I love lambda expressions ^_^

    ReplyDelete
    Replies
    1. Thats very clever actually! :) thanks for sharing

      Delete
  2. Heritage Cabs offers service like Jaipur Car rentalJaipur car hire, taxi hire jaipur car rental service in jaipur @ Rs 8/Km

    ReplyDelete
  3. Gym nearby Gopalpura by Incarnation The Health Club we provide Gym service in Gopalpura bypass (Jaipur), best gym service nearby Gopalpura & Gym service near gopalpura bypass

    ReplyDelete
  4. Digital Markeing Company In Jaipur

    Digitalopeners is the best Digital marketing company in jaipur provide Digital Marketing Service in Jaipur and other web services. SEO, SMO, SMM, PPC, Email Marketing, Digital Marketing Service in Jaipur.

    ReplyDelete
  5. Tempo Traveller Rental in Jaipur
    Tempo Traveller Rental Jaipur, Tempo Traveller Hire Jaipur for outstation & sightseeing Purpose, Book Tempo traveller Rental jaipur, Tempo traveller in Jaipur

    ReplyDelete