【问题标题】:If a character in string is found before another character如果在另一个字符之前找到字符串中的一个字符
【发布时间】:2016-10-18 18:42:00
【问题描述】:

我正在尝试找出一种方法来查看字符串中的一个字符是否在另一个字符之前获取和输出。说:

v="Hello There"
x=v[0]

if "Hello" in x:
    print("V consists of '"'Hello'"'")
        if "There" in x:
             print("Hello comes before There)

if "There" in x:
    print("V consists of '"'There'"'")
        if "Hello" in x:
             print("There comes before Hello")

我想要得到的是“Hello come before there”,尽管当我输入它时它似乎不起作用。非常感谢帮助。

输出会显示Hello出现在there之前的原因是因为脚本是从上到下读取的,这只是对这一事实的利用。

如果其中任何一个没有任何意义,请随时在答案部分与我联系。

【问题讨论】:

    标签: python string variables if-statement


    【解决方案1】:

    对于字符串“s”,s.find(substring) 返回以substring 开头的s 的最低索引

    if s.find('There') < s.find('Hello'):
        print('There comes before Hello')
    

    【讨论】:

      【解决方案2】:

      正如帕特里克所说,这是可行的:

      if s.find('There') < s.find('Hello'):
          print('There comes before Hello')
      

      但是,这只有在两个单词都出现在字符串中时才有效,否则它们的位置将为 -1(即 'There' 不存在,因此它将是 -1

      这里的代码将以更通用的方式工作:

      if s.find('There') < s.find('Hello') and s.find('There') > 0:
          print('There comes before Hello')
      

      【讨论】:

      • 想想>= 0
      【解决方案3】:
      v="Hello There".split()                    #splitting the sentence into a list of words ['Hello', 'There'], notice the order stays the same which is important
                                                 #got rid of your x = v[0] since it was pointless
      if "Hello" in v[0]:                        #v[0] == 'Hello' so this passes
          print("V consists of '"'Hello'"'")
          if "There" in v[1]:                    #v[1] == 'There' so this passes. This line had indentation errors
              print("Hello comes before There")  # This line had indentation errors
      
      if "There" in v[0]:                        #v[0] == 'Hello' so this fails
          print("V consists of '"'There'"'")
          if "Hello" in v[1]:                    #v[1] == 'There' so this fails. This line had indentation errors
              print("There comes before Hello")  # This line had indentation errors
      

      用一些 cmets 修复了您的代码,以向您展示正在发生的事情和未发生的事情。你也有缩进错误。

      如果您想要更好的编码练习,请参阅 Patrick 的回答。我只是想告诉你你做错了什么

      【讨论】:

        【解决方案4】:

        假设您的需求与您在问题详细信息中暗示的一样简单,那么应该这样做 -

        v = "Hello There"
        
        # Change s1 and s2 as you please depending on your actual need.
        s1 = "Hello"
        s2 = "There"
        
        if s1 in v and s2 in v:
            # Refer - https://docs.python.org/2/library/string.html#string.find
            if v.find(s1) < v.find(s2):
                print(s1 + " comes before " + s2)
            else:
                print(s2 + " comes before " + s1)
        

        【讨论】:

          猜你喜欢
          • 2016-02-29
          • 1970-01-01
          • 1970-01-01
          • 2018-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多