【问题标题】:Searching for a word within a list of lists在列表列表中搜索单词
【发布时间】:2013-11-16 04:10:34
【问题描述】:

我正在尝试编写一个函数,该函数将搜索列表列表以查找用户给出的单词或片段并返回包括该单词在内的所有单元。

这是我目前所拥有的:

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

我该如何解决这个问题?

非常感谢!!

【问题讨论】:

  • 我假设if .. else 块在for 循环中。尝试打印i 的值。
  • 是的,他们是——我的错。仍然,除了“对不起,什么都没找到”之外什么都没有。
  • 试试this one
  • @user2850734 你找到解决方案了吗?

标签: sublist


【解决方案1】:
'''
 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"

【讨论】:

    【解决方案2】:

    我建议您在搜索过程中将字符串和stockdata 中的字符串都转换为大写,以便能够同时检测到Computercomputer

    如果没有找到结果,您还应该打印sorry, not found,并且我添加了一个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)
    

    产生: 搜索词 abaxis

    yes
    results :  ['ABAX']
    

    注意:如果可能,请提供您的股票数据列表样本,以确保其有效

    【讨论】:

    • 仍然不起作用 - 这是我的数据列表的示例。
    • [['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日本指数基金', '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 如果对您有帮助,请随时 accept
    【解决方案3】:

    如果您正在搜索列表列表,除非我误解了您的问题,否则您需要另一个 for 循环。到目前为止,K DawG 给出了最好的答案。不幸的是,我不能投票。

    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)
    

    返回:

    computer
    sorry, nothing found
    sorry, nothing found
    mac Computers
    

    【讨论】: