Is there three in a row?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deedee2
    New Member
    • May 2010
    • 1

    #1

    Is there three in a row?

    Hi there,
    Im having an issue starting out a problem.


    I am trying to take a list of poisonous snakes, and my python function needs to take this list of snakes and a given plane name with a list of things. It will then return the number of matches between the snakes and the plane lists.
    Code:
    Snakes = ['Diamondback', 'Rattlesnake', 'Cottonmouth', 'Cobra', 'Black Mamba', 'Viper', 'coral', 'taipan', 'eastern brown']
    
    plane1= ['passenger', 'fbi agent', 'black mamba', 'samuel l jackson', 'eastern brown', 'cobra']

    snakes_on_a_pla ne(snakes, plane1) returns 3.


    I was thinking to iterate through and use a source like message passing, but not sure how to start message passing on python .


    thanks!
    Last edited by bvdet; May 5 '10, 11:51 AM. Reason: Add code tags around assignments
  • v0id
    New Member
    • May 2010
    • 3

    #2
    Code:
    def snakes_on_a_plane(snake,plane):
        return len(set(snakes).intersection(set(plane)))
    you'll need to deal w/ case sensitive stuff but basically thats it.
    Last edited by bvdet; May 5 '10, 11:56 AM. Reason: Add code tags

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      That's it v0id - and this takes care of the case:
      Code:
      set([s.lower() for s in Snakes]).intersection(set([s.lower() for s in plane1]))

      Comment

      • Nonemsludo
        New Member
        • Apr 2010
        • 15

        #4
        try list comprehension and change all the string to a specific case
        >>> Snakes = ['Diamondback', 'Rattlesnake', 'Cottonmouth', 'Cobra', 'Black Mamba', 'Viper', 'coral', 'taipan', 'eastern brown']
        >>> plane1= ['passenger', 'fbi agent', 'black mamba', 'samuel l jackson', 'eastern brown', 'cobra']
        >>> d=len([x for x in Snakes if x==x in plane1])

        Comment

        Working...