[英]How do I extract strings with three forward slashes?
I want to print out the items with three forward slashes as in my desired output.我想用三个正斜杠打印出我想要的输出中的项目。
Everything I try to find is extracting the data in between the slashes or just finding the ones with a slash.我试图找到的一切都是提取斜线之间的数据,或者只是找到带有斜线的数据。
data = ['int', '3/1/2/8', '4/2/1', '5/6/9/2',
'4/1', '9/2/1', '1/4/8/6', 'prod', ]
for info in data:
if '%d/%d/%d/%d' in info:
print(info)
Desired outcome:期望的结果:
3/1/2/8
5/6/9/2
1/4/8/6
A regular expression can help: 正则表达式可以帮助:
import re
out=[]
data=['int', '3/1/2/8', '4/2/1', '5/6/9/2','4/1', '9/2/1', '1/4/8/6', 'prod' ]
for i in data:
if(re.match(".*/.*/.*/.*",i)):
out.append(i)
print(out)
Output from this: 输出结果:
['3/1/2/8', '5/6/9/2', '1/4/8/6']
Use .count() as follows: 使用.count()如下:
data = ['int', '3/1/2/8', '4/2/1', '5/6/9/2',
'4/1', '9/2/1', '1/4/8/6', 'prod', ]
for info in data:
if info.count('/') == 3:
print(info)
You have just to make a function that checks if there is 4 / 您只需要创建一个检查是否存在4 /的函数即可。
for i in data:
if len(i.split("/")) == 4:
print(i)
data = ['int', '3/1/2/8', '4/2/1', '5/6/9/2', '4/1', '9/2/1', '1/4/8/6', 'prod', ] for info in data: if info.count('/') == 3: print(info)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.