Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表列表中搜索字符_Python_List - Fatal编程技术网

Python 在列表列表中搜索字符

Python 在列表列表中搜索字符,python,list,Python,List,我有一个列表列表,我想返回那些具有特定字符的子列表 如果列表为: lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']] 我想检索['g',j']“或它的位置”如果我使用j或g进行搜索,首先变量中有一个关键字错误-列表是一个关键字,请尝试我的列表 这可用于返回所需的列表: #Try this my_list = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']] def check_for_le

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

如果列表为:

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

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

进行搜索,首先变量中有一个关键字错误-列表是一个关键字,请尝试我的列表

这可用于返回所需的列表:

#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']
>>> 

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

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']]

试试这个:-

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'))
lst=['a','e'],['g','j'],['m','n','w'],['z']]
def搜索(规格字符):
对于lst中的子列表:
如果子列表中的spec_char:
返回子列表
返回错误
打印搜索('g')
>>>['g','j']

这里我将“f”作为要搜索的字符

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

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

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)
lst=['a','e'],['g','j'],['m','n','w'],['z']]
ch=输入(“输入字符”)#或直接在“”中键入字符
#拉姆达:
#过滤器(,)
r=列表(过滤器(λlst:ch in lst,lst))
印刷品(r)

注意:为了查看lambda和filter函数返回的值,我将结果存储在一个列表中并打印最终输出。

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

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']

如果您还想知道列表的位置,请发表评论。我已经设法手动完成了这项工作,但我认为有一种有效的方法可以做到这一点。另外,我刚才用这个列表来澄清我的问题,谢谢!这回答了你的问题吗?
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']