Python 使用第一个元素的值获取结果集的第二个和第三个元素的值

Python 使用第一个元素的值获取结果集的第二个和第三个元素的值,python,postgresql,Python,Postgresql,结果集为: acur.execute("Select id,firstname,lastname from tbl_emp") tk = acur.fetchall() 如何使用第一个元素的值搜索此结果集 假设我使用2搜索,结果将是c和d。为什么不首先过滤它,而不是获取所有记录: tk = [(1,'a','b'),(2,'c','d'),(3,'e','f')] 如果您只是查看元组: acur.execute(""" SELECT firstname,

结果集为:

acur.execute("Select id,firstname,lastname from tbl_emp")
tk = acur.fetchall()
如何使用第一个元素的值搜索此结果集


假设我使用2搜索,结果将是c和d。为什么不首先过滤它,而不是获取所有记录:

tk = [(1,'a','b'),(2,'c','d'),(3,'e','f')]

如果您只是查看元组:

acur.execute("""
    SELECT 
        firstname, 
        lastname 
    FROM 
        tbl_emp 
    WHERE 
        id = %s""", (2, ))
firstname, lastname = acur.fetchone()
>>> def search(_list, _value):
...     for item in _list:
...             if item[0] == _value:
...                     return item[1], item[2]
... 
>>> search(tk, 2)
('c', 'd')
>>> search(tk, 3)
('e', 'f')
>>> search(tk, 4)
>>>