在Python中搜索二维列表中的元素

在Python中搜索二维列表中的元素,python,Python,我有一个二维列表,大约有1000个元素。它看起来像这样: myList = [['1', 'John', 'Doe', 'jdoe@email.com', '234-35-2355'], ['2', 'Rebecca', 'Swan', 'rswan@email.com', '244-56-4566'], ['3', 'Terry', 'Smith' 'tsmith@email.com', '345-45-1234']] 索引[1]是他们的名字,我想搜索这个列表(有1000人,为了简单起见只使

我有一个二维列表,大约有1000个元素。它看起来像这样:

myList = [['1', 'John', 'Doe', 'jdoe@email.com', '234-35-2355'], ['2', 'Rebecca', 'Swan', 'rswan@email.com', '244-56-4566'], ['3', 'Terry', 'Smith' 'tsmith@email.com', '345-45-1234']]
索引[1]是他们的名字,我想搜索这个列表(有1000人,为了简单起见只使用了3人),看看他们是否在列表中,如果在列表中,不使用Numpy打印他们的信息

到目前为止,我已经:


firstName = input('Enter the first name of the record: ')

row = len(myList)

found = False
for row in my list:
    for element in row:
        if element == firstName:
            found = True
            break

    if found:
        break

if found:
    print('User ID: ', myList[0])
    print('First Name: ', myList[1])
    print('Last Name: ', myList[2])
    print('Email: ', myList[3])
    print('Login IP: ', myList[4])

else:
    print('There is no record found with first name', firstName)

现在,这似乎正在努力寻找此人是否在那里,但我在打印信息后遇到了麻烦,因为我不知道如何找到此人的索引,我相信如果我有索引,打印的内容将类似于
myList[index][1]

编辑:好的,我看到这是一个将myList[1]更改为row[1]的简单修复。
现在,假设您搜索一个姓名,列表中的两个人姓名相同,并且您希望打印这两组信息,我将如何执行此操作?

多种可能的方法之一:

def findByName(name, lst):
    return filter(lambda x: x[1] == name, lst)

for item in findByName("John", myList):
    print(item)
这就产生了

['1', 'John', 'Doe', 'jdoe@email.com', '234-35-2355']

或直接使用listcomp:

persons = [entry for entry in myList if entry[1] == name]

你不需要索引。找到名称并跳出for循环后,您有了要分配给
行的对象
仍应指向您需要的信息。如果你也希望索引与外部for循环一起使用。好的,我明白了,谢谢!现在假设你搜索一个名字,列表中的两个人有相同的名字,你想打印这两组信息。。。我该怎么做呢?保留所有索引供以后使用,或者在找到它们时打印出来。这能回答你的问题吗。。对于这个问题,我们应该这样做。