Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/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 2.7 如果输入与嵌套列表中的子列表元素匹配,如何获取所有子列表作为输出_Python 2.7_Nested Lists - Fatal编程技术网

Python 2.7 如果输入与嵌套列表中的子列表元素匹配,如何获取所有子列表作为输出

Python 2.7 如果输入与嵌套列表中的子列表元素匹配,如何获取所有子列表作为输出,python-2.7,nested-lists,Python 2.7,Nested Lists,下面的代码是若输入和子列表的元素匹配,那个么将子列表作为输出。37.21匹配两次,但给出一个子列表作为输出。如何在输出中获取两个子列表 nested_list = [['A', 37.21], ['B', 37.21], ['C', 37.2], ['D', 41], ['E', 39]] match = 37.21 def search_nested(mylist, val): for i in range(len(mylist)): for j in range(le

下面的代码是若输入和子列表的元素匹配,那个么将子列表作为输出。37.21匹配两次,但给出一个子列表作为输出。如何在输出中获取两个子列表

nested_list = [['A', 37.21], ['B', 37.21], ['C', 37.2], ['D', 41], ['E', 39]]
match = 37.21
def search_nested(mylist, val):
    for i in range(len(mylist)):
        for j in range(len(mylist[i])):
            #print i,j
            if mylist[i][j] == val:
                return mylist[i]
print search_nested(nested_list, match)

如果需要,可以将for循环放在返回i的方法中,而不是直接打印;谢谢你,德拉科!如果它是以标记作为答案的,请,否则它会作为未回答的问题飘进来,我已经讨厌浏览未回答的问题时发现80%的问题实际上得到了回答,只是浪费了专业人士的时间,因为作者无法分别对其进行标记…我已经对其进行了高估。但由于我的声誉,它不可见。还有什么我需要做的吗?接受答案,答案附近应该有一个按钮(勾选向上投票/向下投票按钮下方的右键),如果你愿意,你可以将该for循环放在返回i的方法中,而不是直接打印;谢谢你,德拉科!如果它是以标记作为答案的,请,否则它会作为未回答的问题飘进来,我已经讨厌浏览未回答的问题时发现80%的问题实际上得到了回答,只是浪费了专业人士的时间,因为作者无法分别对其进行标记…我已经对其进行了高估。但由于我的声誉,它不可见。还有什么我需要做的吗?接受答案,答案附近应该有按钮(在向上投票/向下投票按钮下方右勾选)
nested_list = [['A', 37.21], ['B', 37.21], ['C', 37.2], ['D', 41], ['E', 39]]
match = 37.21
for i in nested_list:
    if match in i:
        print(i)