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

    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    For loop on items in a list


    'see spot run spot runs fast in book' is the string I am testing.

    It's trying to get a result of...
    ['see', 'spot', 'run', 'spot', 'runs', 'fast', 'in', 'book']
    ['see', 'spot', 'run', 'spot', 'runs', 'fast']
    ['in']
    ['book']

    ...but i is not what I need to be using I take it.
    I'm getting this...

    ['see', 'spot', 'run', 'spot', 'runs', 'fast', 'in', 'book']
    ['see', 'spot', 'run', 'spot', 'runs', 'fast', 'in', 'book']
    []
    []

    Code:
    import os
    import sys
    import string
    
    prg_run = True
    
    while prg_run == True :
        
        lrn_str = raw_input('  Enter A String: ')
    
        if ' in ' in str(lrn_str) or \
           ' is ' in str(lrn_str) or \
           ' was ' in str(lrn_str) or \
           ' on ' in str(lrn_str) or \
           ' maybe ' in str(lrn_str) :
            
            x = "".join([c for c in lrn_str if c in string.letters or c in string.whitespace or c in string.digits]) 
            x = x.lower()
            x = x.strip(' ')
            
            z = list(x)
            wrd_str = x.split(' ')
            print wrd_str
            
            beg_str = []
            key_wrd = []
            end_str = []
            
            for i in wrd_str:
                
                key_usd = False
                
                if key_usd == False and \
                   i != 'in' or \
                   i != 'is' or \
                   i != 'was' or \
                   i != 'on' or \
                   i != 'maybe':
                    beg_str.append(i)
                    
                elif i == 'in' or \
                     i == 'is' or \
                     i == 'was' or \
                     i == 'on' or \
                     i == 'maybe' :
                    key_wrd.append(i)
                    key_usd = True
                    
                else:
                    end_str.append(i)
                    
            print beg_str
            print key_wrd
            print end_str
            
        else:
            break
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,899
    Rep Power
    481
    Do not embed your word list into your program!
    Always True! (i != 'in') or (i != 'is')

    Code:
    import os
    import sys
    import string
    
    prg_run = True
    
    charset = set(string.letters+string.whitespace+string.digits)
    words = 'in is was on maybe'.split()    # do not repeat this word list!!!!
    words_with_spaces = [' %s '%word for word in words]
    
    while prg_run: #  == True   is redundant
        lrn_str = raw_input('  Enter A String: ') # lrn_str is already a string
        lrn_str = lrn_str.lower()                 # lower case later is futile
        if any(sw in lrn_str for sw in words_with_spaces): # learn the python functions:  any
            x = ''.join(c for c in lrn_str if c in charset) # notice I've used a generator, not a list comprehension.
            x = x.strip(' ')
    
            z = list(x)
            wrd_str = x.split(' ')
            print wrd_str
    
            beg_str = []
            key_wrd = []
            end_str = []
    
            key_usd = False ######### this statement moved.  Useless 
    
            for i in wrd_str:
    
                # key_usd = False  #complete bogosity if the statement is here
    
    
                # this is always true
                (i != 'in') or (i != 'is')
    
    
                if (not key_usd) and i not in words:
                    beg_str.append(i)
    
                elif i in words:
                    key_wrd.append(i)
                    key_usd = True
    
                else:
                    end_str.append(i)
    
            print beg_str
            print key_wrd
            print end_str
    
        else:
            break
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo