Python二列出查找索引值
发布时间:2020-12-20 11:21:49 所属栏目:Python 来源:网络整理
导读:listEx = ['cat *(select: "Brown")*','dog','turtle','apple']listEx2 = ['hampter',' bird','monkey','banana','cat']for j in listEx2: for i in listEx: if j in i: print listEx.index(j) 我想要完成的是在listEx中搜索listEx2中的项目.如果在listEx中
listEx = ['cat *(select: "Brown")*','dog','turtle','apple'] listEx2 = ['hampter',' bird','monkey','banana','cat'] for j in listEx2: for i in listEx: if j in i: print listEx.index(j) 我想要完成的是在listEx中搜索listEx2中的项目.如果在listEx中找到listEx2中的项目,我想知道如何打印listEx中listEx2中找到的项目的索引值.谢谢! 解决方法
只需使用枚举:
listEx = ['cat *(select: "Brown")*','cat'] for j in listEx2: for pos,i in enumerate(listEx): if j in i: print j,"found in",i,"at position",pos,"of listEx" 这将打印 cat found in cat *(select: "Brown")* at position 0 of listEx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |