【问题标题】:Search for a char in list of lists在列表列表中搜索字符
【发布时间】:2018-07-30 02:15:32
【问题描述】:

我有一个列表列表,我想返回那些具有特定字符的子列表。

如果列表是:

lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]

如果我使用jg 进行搜索,我想检索['g', 'j']“或它的位置”

【问题讨论】:

标签: python list


【解决方案1】:

首先,您的变量中存在关键字错误 - 列表是关键字,请尝试 my_list。

这适用于返回您想要的列表:

#Try this
my_list = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
def check_for_letter(a_list,search):
    for i in a_list[:]:
        if search in a_list[0]:
            return a_list[0]
        else:
            a_list[0] = a_list[1]

下面的会话:

>>> check_for_letter(my_list,"j")
['g', 'j']
>>> 

【讨论】:

  • 如果您也想要列表的位置,请发表评论。
  • 我已经设法手动完成,但我认为有一种有效的方法可以做到这一点。附言我只是用 List 来澄清我的问题,谢谢!
【解决方案2】:

这是一种方式。它甚至适用于重复。

lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]

def searcher(lst, x):
    for i in range(len(lst)):
        if x in lst[i]:
            yield i

list(searcher(lst, 'g'))                        # [1]
list(map(lst.__getitem__, searcher(lst, 'g')))  # [['g', 'j']]

【讨论】:

    【解决方案3】:

    试试这个:-

    lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
    def search(search_char):
        result = [x for x in lst if search_char in x]
        return result
    print(search('g'))
    

    【讨论】:

      【解决方案4】:
      lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
      
      spec_char = input("What character do you want to find?: ")#ask for a character to find
      
      def find_char(spec_char):
          for list_count, each_list in enumerate(lst):      #iterates through each list in lst
              if spec_char in each_list:                    #checks if specified character is in each_list
                  return spec_char, lst[list_count]         #returns both the character and list that contains the character
      
      def main():                                           #good habit for organisation
          print(find_char(spec_char))                       #print the returned value of find_char
      
      if __name__ == '__main__':                            
          main()
      

      【讨论】:

        【解决方案5】:
        lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
        
        def search(spec_char):
            for subList in lst:
                if spec_char in subList:
                    return subList
            return False
        
        print search('g')
        >>> ['g', 'j']
        

        【讨论】:

          【解决方案6】:
          y=[['a','b'],['c','d'],['e','f'],['f']]
          result=[x for x in y if 'f' in x])
          

          这里我把'f'作为要搜索的字符

          【讨论】:

            【解决方案7】:

            或者,我们也可以使用 lambda 和 filter 函数。

            lambda 和 filter 函数的基础知识可以在 python 文档中找到。

            lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
            ch = input('Enter the character') # or directly type the character in '' 
            
            # lambda <parameter to the function>: <value to be returned>
            # filter(<lambda function to check for the condition>, <sequence to iterate over>)
            
            r = list(filter(lambda lst: ch in lst,lst))
            print(r)
            

            注意:要查看 lambda 和过滤器函数返回的值,我将结果存储在列表中并打印最终输出。

            【讨论】:

              【解决方案8】:

              以下是您的问题的解决方案和解释。请查看此答案底部的图片以进一步说明并查看输出。

              lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']] #So this is your list
              character=input("Enter the character: ") #The character you are searching for
              for a in lst: #it means that variable [a] is an item in the list [lst]
                  for b in a: #it means that variable [b] is an item in the list [a]
                      if(b==character): #To check if the given character is present in the list
                           print(a) #Finally to print the list in which the given character is present
              

              至此,代码部分就结束了。现在,让我们看看输出会是什么。

              C:\Users\User\Desktop\python>coc.py
              
              Enter the character: a
              ['a', 'e']
              
              C:\Users\User\Desktop\python>coc.py
              
              Enter the character: w
              ['m', 'n', 'w']
              

              Click here to view the image of my code and output

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-03-12
                • 1970-01-01
                • 1970-01-01
                • 2021-03-02
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多