所有人
我有以下问题。我有一个元组列表,如果元组包含变量,我想查找元组的索引。以下是我目前掌握的一个简单代码:
items = [('show_scllo1', '100'), ('show_scllo2', '200')]
s = 'show_scllo1'
indx = items.index([tupl for tupl in items if tupl[0] == s])
print(indx)
但是我得到了错误:
indx = items.index([tupl for tupl in items if tupl[0] == s])
ValueError: list.index(x): x not in list
我看了几篇类似的文章,但它们没有帮助我解决问题。你知道我做错了什么吗?
您正在检查
items
中是否存在不包含list
的list
。相反,您应该为找到感兴趣项的每个索引创建一个list
:下面将返回第一项为
s
的元组的索引似乎你想要这个值,所以你要的是索引。
可以使用^{} 在列表中搜索下一个匹配值:
所以你根本不需要索引。
相关问题 更多 >
编程相关推荐