#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    3

    String problem: find the exact string


    Hi, I'm having a problem about string. I'd like to ask you guys if there's a way to match the exact same string. Here's an example.

    Code:
    phrase = "play music"
    
    CommandPhrase = ["can play music", "could play music"]
    
    Command1Phrase = "play music", "open music"
    
    NP = [y for y in CommandPhrase if phrase in y]
    NP1 = [y for y in CommandPhrase if phrase in y]
    and result is

    Code:
    print NP:['can play music', 'could play music']
    print NP1: ['play music']
    The problem is phrase = "play music", so it should be only NP1 matched perfectly to phrase, but in NP still printed something which is consisted of 'play music' also.

    Is there a way to make my string match perfectly? because I want the result to be
    NP -- []
    Np1 -- ['play music']

    Helps or any suggestions would be much appreciated. Thank you
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    First, I presume a typo in your example. Note the 1 in Command1Phrase:
    Code:
    NP1 = [y for y in Command1Phrase if phrase in y]
    Mostly, use `==' instead of `in'.
    Code:
    phrase = "play music"
    CommandPhrase = ["can play music", "could play music"]
    Command1Phrase = "play music", "open music"
    NP = [y for y in CommandPhrase if phrase == y]
    NP1 = [y for y in Command1Phrase if phrase == y]
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    3
    Ohh, you're right!!!! thank you very much !!!

IMN logo majestic logo threadwatch logo seochat tools logo