在列表列表中搜索char - Python

时间:2018-02-19 19:36:57

标签: python python-3.x list

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

如果列表是:

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

如果我使用['g', 'j']j

进行搜索,我想要检索g“或其位置”

8 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

 import excel "Z:/Data/`year'/GIS Class Data Report1_`year'.xlsx", sheet("Sheet1") firstrow clear 

答案 2 :(得分:0)

试试这个: -

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

答案 3 :(得分: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()

答案 4 :(得分: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']

答案 5 :(得分:0)

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

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

答案 6 :(得分: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和过滤器函数返回的值,我将结果存储在列表中并打印最终输出。

答案 7 :(得分: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']

Click here to view the image of my code and output