python-根据部分字符串在列表中查找索引位置 [英] python - find index position in list based of partial string

查看:1885
本文介绍了python-根据部分字符串在列表中查找索引位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"]

我需要所有包含'aa'的项目的索引位置.我在将enumerate()与部分字符串匹配结合在一起时遇到麻烦.我什至不确定我是否应该使用枚举.

I need the index position of all items that contain 'aa'. I'm having trouble combining enumerate() with partial string matching. I'm not even sure if I should be using enumerate.

我只需要返回索引位置:0,2,5

I just need to return the index positions: 0,2,5

推荐答案

indices = [i for i, s in enumerate(mylist) if 'aa' in s]

这篇关于python-根据部分字符串在列表中查找索引位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆