1

I'm trying to write a function that will search a list of lists to find a word or fragment given by the user and return all units including that word.

here's what I have so far:

def cat(dogs):
"""
 searches for cat names in dogs
"""
  search = raw_input("search for: ")
  word = search[0].upper() + search[1:]

  for i in range(len(dogs)):
    if word in dogs[i]: 
      print "yes"
    else:
     print "sorry, nothing found"
     return

how do I fix this?

Thanks so much!!

3 Answers3

0

I would suggest that you should convert your both string and the strings in stockdata to uppercase in the search process so it'd be able to detect both Computer and computer

You should also print sorry, not found if no result was found and I've added a results variable to see the search results.

def company(stockdata):
    """
     searches for companies with the word inputted and gives back full
     company names and ticker tags
     """
    found = False
    results = []
    search = raw_input("search for: ")
    word = search.upper()

    for i in range(len(stockdata)):
        if word in stockdata[i][0].upper() or word in stockdata[i][1].upper(): # I've used stockdata[i][0] because the string is in a list of list
            found = True
            results.append(stockdata[i][0])
    if found:
        print 'yes'
        print 'results : ',results
    else:
        print "sorry, nothing found"

stock  = [['AAME', 'Atlantic American Corporation', '2013-11-04', 4.04, 4.05, 4.01, 4.05, 5400.0, 4.05], ['AAON', 'AAON Inc.', '2013-11-04', 27.28, 27.48, 27.08, 27.32, 96300.0, 27.32], ['AAPL', 'Apple Inc.', '2013-11-04', 521.1, 526.82, 518.81, 526.75, 8716100.0, 526.75], ['AAWW', 'Atlas Air Worldwide Holdings', '2013-11-04', 38.65, 39.48, 38.65, 38.93, 490500.0, 38.93], ['AAXJ', 'iShares MSCI All Country Asia ex Japan Index Fund', '2013-11-04', 60.55, 60.55, 60.3, 60.48, 260300.0, 60.48], ['ABAX', 'ABAXIS Inc.', '2013-11-04', 36.01, 36.91, 35.89, 36.2, 208300.0, 36.2]]
company(stock)

produces: for the search term abaxis

yes
results :  ['ABAX']

Note: please provide a sample of your stock data list which is passed onto the function if possible as to ensure this works

K DawG
  • 13,287
  • 9
  • 35
  • 66
  • still doesn't work - here's a sample of my data list. – user2850734 Nov 16 '13 at 04:47
  • [['AAME', 'Atlantic American Corporation', '2013-11-04', 4.04, 4.05, 4.01, 4.05, 5400.0, 4.05], ['AAON', 'AAON Inc.', '2013-11-04', 27.28, 27.48, 27.08, 27.32, 96300.0, 27.32], ['AAPL', 'Apple Inc.', '2013-11-04', 521.1, 526.82, 518.81, 526.75, 8716100.0, 526.75], ['AAWW', 'Atlas Air Worldwide Holdings', '2013-11-04', 38.65, 39.48, 38.65, 38.93, 490500.0, 38.93], ['AAXJ', 'iShares MSCI All Country Asia ex Japan Index Fund', '2013-11-04', 60.55, 60.55, 60.3, 60.48, 260300.0, 60.48], ['ABAX', 'ABAXIS Inc.', '2013-11-04', 36.01, 36.91, 35.89, 36.2, 208300.0, 36.2]] – user2850734 Nov 16 '13 at 04:48
0
'''
 searches for companies with the word input and gives back full
 company names and ticker tags  
'''
def company(stockdata):
    searchString = raw_input("search for: ")
    flatList = [item.upper() for sublist in stockdata for item in sublist]
    if any(searchString.upper() in s for s in flatList):
        print "Yes"
    else:
        print "sorry, nothing found"
Spade
  • 2,220
  • 1
  • 19
  • 29
0

If you're searching a list of lists, you need another for loop unless I'm misunderstanding your question. K DawG has given the best answer so far. Unfortunately I can't upvote it.

def company(stockdata):
    search = raw_input("search for: ")
    word = search.upper()

    for i in range(len(stockdata)):
        for j in range(len(stockdata[i])):
            if word in stockdata[i][j].upper(): 
                print stockdata[i][j]
            else:
                print "sorry, nothing found"
    return

data = [["computer", "cheese"], ["apple"], ["mac Computers"]]

company(data)

returns:

computer
sorry, nothing found
sorry, nothing found
mac Computers