在列表的列表中查找字符

5
我将翻译如下:

我有一个子列表的列表,我想返回包含特定字符的子列表。

如果列表如下:

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

如果我使用 j 或者 g 进行搜索,我希望能够检索到['g', 'j'],或者它们的位置。


这个回答解决了你的问题吗?在元组列表中查找元素 - Georgy
8个回答

1
尝试这个:-
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'))

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

0
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()

0

首先,您的变量中存在关键字错误 - list是一个关键字,请尝试使用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']
>>> 

如果您需要列表的位置,请在评论中说明。 - aPythonUser
我已经手动完成了,但我认为有更有效的方法来完成它。附言:我只是使用列表来澄清我的问题,谢谢! - Ali H. El-Kassas

0

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

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

0
y=[['a','b'],['c','d'],['e','f'],['f']]
result=[x for x in y if 'f' in x])

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


0

另外,我们也可以使用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和filter函数返回的值,我将结果存储在一个列表中并打印最终输出。

0
以下是您问题的解决方案和说明。请查看答案底部的图片以获得进一步的澄清并查看输出结果。
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']

点击此处查看我的代码和输出的图片


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接