【发布时间】:2018-07-30 02:15:32
【问题描述】:
我有一个列表列表,我想返回那些具有特定字符的子列表。
如果列表是:
lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
如果我使用j 或g 进行搜索,我想检索['g', 'j']“或它的位置”
【问题讨论】:
-
这能回答你的问题吗? Find an element in a list of tuples
我有一个列表列表,我想返回那些具有特定字符的子列表。
如果列表是:
lst = [['a', 'e'], ['g', 'j'], ['m', 'n', 'w'], ['z']]
如果我使用j 或g 进行搜索,我想检索['g', 'j']“或它的位置”
【问题讨论】:
首先,您的变量中存在关键字错误 - 列表是关键字,请尝试 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']
>>>
【讨论】:
这是一种方式。它甚至适用于重复。
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']]
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()
【讨论】:
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']
【讨论】:
y=[['a','b'],['c','d'],['e','f'],['f']]
result=[x for x in y if 'f' in x])
这里我把'f'作为要搜索的字符
【讨论】:
或者,我们也可以使用 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 和过滤器函数返回的值,我将结果存储在列表中并打印最终输出。
【讨论】:
以下是您的问题的解决方案和解释。请查看此答案底部的图片以进一步说明并查看输出。
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']
【讨论】: