【问题标题】:Count occurrances of a word before another phrase in python 3 [duplicate]在python 3中的另一个短语之前计算一个单词的出现次数[重复]
【发布时间】:2018-12-12 19:50:04
【问题描述】:

我想计算\n 在字符串(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,) 中出现在短语Shaping the Future 之前的次数。有没有不拆分字符串的方法?

这种情况下的输出应该是 3

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    您可以将字符串切分到您感兴趣的子字符串,然后使用count

    s = """(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,)""" 
    s[:s.index("Shaping the Future")].count('\n')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      相关资源
      最近更新 更多