首页 > 解决方案 > 如何在数组/列表中按索引查找值?

问题描述

如果数组是['VyJXcp', 'https://google.com'],我如何通过索引获取“https://google.com”?

我尝试了以下方法:

@app.route('/<id>')
def redirect(id):
    target = Shrtn.query.filter_by(key=id).all()
    target2 = target[1]
    return str(target2)

和 Shrtn.query.filter_by(key=id).all() 结果很好(上面粘贴的那个),当我这样做时,return str(target)它确实有效,但是当我使用return str(target2)它时,我得到一个 IndexError;IndexError: list index out of range

返回正确字符串的正确方法是什么?

标签: pythonsqlalchemy

解决方案


如果数组是 ['VyJXcp', 'https://google.com'] 所以

arr = ['VyJXcp', 'https://google.com']    ?

我怎样才能通过索引获得“https://google.com”

arr[1]

结果是

Out[11]: 'https://google.com'

推荐阅读