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:
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 /
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.