Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/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 在sql查询返回的数组中查找项_Python_List_Find - Fatal编程技术网

Python 在sql查询返回的数组中查找项

Python 在sql查询返回的数组中查找项,python,list,find,Python,List,Find,我有一个数组(列表?),如下所示,它是sql查询的结果 lstLineRouteKey = [('1203/1000/2', 1, 'DVN 1000'), ('1212/1000/2', 2, 'RKS 1000'), ('1189/1000/2', 3, 'USH 1000'), ('1201/1000/2', 4, 'ANA 1000'), ('1232/1000/2', 5, 'CLB 1000'), ('1207/1000/2', 6, 'HRY 1000'), ('1162/1000

我有一个数组(列表?),如下所示,它是sql查询的结果

lstLineRouteKey = [('1203/1000/2', 1, 'DVN 1000'), ('1212/1000/2', 2, 'RKS 1000'), ('1189/1000/2', 3, 'USH 1000'), ('1201/1000/2', 4, 'ANA 1000'), ('1232/1000/2', 5, 'CLB 1000'), ('1207/1000/2', 6, 'HRY 1000'), ('1162/1000/2', 7, 'LBR 1000')]
我要做的是找到'1212/1000/2'的第一个(唯一)实例,并检索它右边的值,2

我用下面的for循环成功地做到了这一点,但它效率很低,因为它在整个列表中循环,即使在找到匹配项之后也是如此

for id,row in enumerate(lstLineRouteKey):
                    lst = list(row)
                    for id,s in enumerate(lst):
                        if lst[0] == '1212/1000/2':
                            ID = str(lst[1]);

到目前为止,我发现的方法(.find、.map)都不成功,因为没有一个列表类似于我上面的结构。非常感谢您的帮助

那么,您正在寻找符合某些条件的第一条记录?您可以使用
next
执行此操作

>>> next(x[1] for x in lstLineRouteKey if x[0] == '1212/1000/2')
2
next
的优点是它非常高效。它将耗尽生成器表达式,返回满足该表达式的第一个值<如果不需要,代码>下一步将不会迭代整个列表

为了可视化发生的事情,考虑一个列表理解表的代码< y= [x](1),如果x[ 0 ]=“1212/1000/2′”< /代码>,生成一个k元素列表。在这种情况下,

next
将返回
y[0]
的值。实际上,中间列表不是生成的!这就是它高效的原因


如果记录可能不存在,
next
可能会引发
StopIteration
。在这种情况下,将默认参数传递给
next
。这里有一个例子

>>> print(next((x[1] for x in lstLineRouteKey if x[0] == '1212/1000/3'), None))
None

那么,你在寻找符合某些条件的第一条记录?您可以使用
next
执行此操作

>>> next(x[1] for x in lstLineRouteKey if x[0] == '1212/1000/2')
2
next
的优点是它非常高效。它将耗尽生成器表达式,返回满足该表达式的第一个值<如果不需要,代码>下一步将不会迭代整个列表

为了可视化发生的事情,考虑一个列表理解表的代码< y= [x](1),如果x[ 0 ]=“1212/1000/2′”< /代码>,生成一个k元素列表。在这种情况下,

next
将返回
y[0]
的值。实际上,中间列表不是生成的!这就是它高效的原因


如果记录可能不存在,
next
可能会引发
StopIteration
。在这种情况下,将默认参数传递给
next
。这里有一个例子

>>> print(next((x[1] for x in lstLineRouteKey if x[0] == '1212/1000/3'), None))
None
您所需要的只是声明:

它终止最近的封闭循环,如果循环有可选的else子句,则跳过该子句

您可以在代码中使用它,如:

lstLineRouteKey = [('1203/1000/2', 1, 'DVN 1000'), ('1212/1000/2', 2, 'RKS 1000'), ('1189/1000/2', 3, 'USH 1000'), ('1201/1000/2', 4, 'ANA 1000'), ('1232/1000/2', 5, 'CLB 1000'), ('1207/1000/2', 6, 'HRY 1000'), ('1162/1000/2', 7, 'LBR 1000')]

my_result = None

for line_route in lstLineRouteKey:
    if line_route[0] == '1212/1000/2':
        my_result = line_route[1]
        break   # on reaching here, your for loop will be terminated 
                # (no further execution) 
您所需要的只是声明:

它终止最近的封闭循环,如果循环有可选的else子句,则跳过该子句

您可以在代码中使用它,如:

lstLineRouteKey = [('1203/1000/2', 1, 'DVN 1000'), ('1212/1000/2', 2, 'RKS 1000'), ('1189/1000/2', 3, 'USH 1000'), ('1201/1000/2', 4, 'ANA 1000'), ('1232/1000/2', 5, 'CLB 1000'), ('1207/1000/2', 6, 'HRY 1000'), ('1162/1000/2', 7, 'LBR 1000')]

my_result = None

for line_route in lstLineRouteKey:
    if line_route[0] == '1212/1000/2':
        my_result = line_route[1]
        break   # on reaching here, your for loop will be terminated 
                # (no further execution) 

一旦在
元组中找到索引
[0]
处的项与您的查询匹配,您就可以将索引
[1]
处的值分配给
ID
,并中断循环

lstLineRouteKey = [('1203/1000/2', 1, 'DVN 1000'), ('1212/1000/2', 2, 'RKS 1000'), ('1189/1000/2', 3, 'USH 1000'), ('1201/1000/2', 4, 'ANA 1000'), ('1232/1000/2', 5, 'CLB 1000'), ('1207/1000/2', 6, 'HRY 1000'), ('1162/1000/2', 7, 'LBR 1000')]

ID = ''

for x, y, z in lstLineRouteKey:
    if x == '1212/1000/2':
        ID = str(y)
        break

一旦在
元组中找到索引
[0]
处的项与您的查询匹配,您就可以将索引
[1]
处的值分配给
ID
,并中断循环

lstLineRouteKey = [('1203/1000/2', 1, 'DVN 1000'), ('1212/1000/2', 2, 'RKS 1000'), ('1189/1000/2', 3, 'USH 1000'), ('1201/1000/2', 4, 'ANA 1000'), ('1232/1000/2', 5, 'CLB 1000'), ('1207/1000/2', 6, 'HRY 1000'), ('1162/1000/2', 7, 'LBR 1000')]

ID = ''

for x, y, z in lstLineRouteKey:
    if x == '1212/1000/2':
        ID = str(y)
        break

@谢谢!是的,这正是我的意思:它引起了我的注意,因为我得到了相同的答案。没关系:)谢谢你,莫伊努丁·夸迪@谢谢!是的,这正是我的意思:它引起了我的注意,因为我得到了相同的答案。没关系:)谢谢你,莫伊努丁·夸迪@不客气。如果您最终选择了这个答案,您可以通过单击灰色复选框将其切换为绿色,将其标记为接受。string var=str(cabinet);因此,对字符串'1212/1000/2'进行硬编码是可行的,但我无法传递字符串变量来代替它。我继续得到一个名称“string var”未定义。@vvPULSEvv您能提出一个新问题吗?我需要更多的信息。@VvPulsev不客气。如果您最终选择了这个答案,您可以通过单击灰色复选框将其切换为绿色,将其标记为接受。string var=str(cabinet);因此,对字符串'1212/1000/2'进行硬编码是可行的,但我无法传递字符串变量来代替它。我继续得到一个名称“string var”未定义。@vvPULSEvv您能提出一个新问题吗?我需要更多的信息。