Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
some_list = ['Name','Surname','R500'] some_list = ['Name','Surname','500']
如果获取列表中包含数字的项目的索引,在这两种情况下我都应该返回 index = 2
我在看类似的东西:
some_list.index(r'%r' % '\d+')
您需要遍历元素:
for i, x in enumerate(my_list): if re.search(r"\d", x): print i
如果您只查找包含数字的第一项,则此方法无需正则表达式即可工作,-1如果没有包含数字的元素,则返回(可以更改为您想要的任何内容):
-1
next((i for i,n in enumerate(some_list) if any(c.isdigit() for c in n)), -1)