简体   繁体   English

如何使用三个正斜杠提取字符串?

[英]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.

相关问题 如何使用 python 的 split() 从正斜杠 substring 拆分带有正斜杠的字符串? - How do I split a string with forward slashes in from the forward slashes substring using split() of python? Python正则表达式提取正斜杠之间的数字 - Python regex extract number between forward slashes 如何在 Windows 上使用 pathlib 输出带有正斜杠的路径? - How can I output paths with forward slashes with pathlib on Windows? 如何在python中转义正斜杠,以便open()将我的文件视为要写入的文件名,而不是要读取的文件路径? - How do I escape forward slashes in python, so that open() sees my file as a filename to write, instead of a filepath to read? 如何从基于三列的 DataFrame 中提取? - How do I extract from the DataFrame based on three columns? 如何将反斜杠转换为正斜杠? - How to convert back-slashes to forward-slashes? 如何用反斜杠而不是正斜杠写我的路径? - How to get my path written with back slashes instead of forward slashes? 从字符串内部提取多个正斜杠后的字符串 - Extract a string after a number of forward slashes from inside a string 按字符串中的正斜杠数将带有字符串作为值的字典排序 - Sort a dictionary with strings as values by number of forward slashes in the string 如何仅从字符串列表中提取浮点数? - How do I extract only floats from a list of strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM