【问题标题】:How to check if list of strings entered are in alphabetical order?如何检查输入的字符串列表是否按字母顺序排列?
【发布时间】:2020-12-29 16:34:16
【问题描述】:

我有一个程序,它从用户输入中获取一系列字符串,如果输入的字符串按字母顺序排列,则应打印“是”,否则应打印“否”。该程序以用户输入一个空输入而结束。当我指定它应该具有的输入数量时,我可以这样做,例如 2:

finished = False
while not finished:
    print("Please enter a string: ")
    s = input()
    x = input()
    if len(s) != 0: 
        if s < x:
            print("Yes")
        else:
            print("No")  
    else:
        finished = True

但是,当可以输入的字符串数量不定时,我似乎无法让我的代码工作。我目前的工作方法是将所有字符串附加到一个列表并在那里执行检查,但我不确定如何编写 if 语句来检查:

lst = []
i = range(len(lst))

finished = False
while not finished:
    print("Please enter a string: ")
    s = input()
    lst.append(s)
    if len(s) != 0:
        if lst[i:] < lst[i: - 1]:
            print("Yes")
        else:
            print("No")
    else:
        finished = True

有没有一种简单的方法可以在不偏离上述预期结构太远的情况下实现这一目标?

【问题讨论】:

    标签: python string list alphabetical


    【解决方案1】:

    lst 是一个字符串列表,对它进行切片语法将为您返回一个字符串列表。但是你想要一个字符串。由于您要追加到列表中,最新追加的字符串将出现在[-1] 索引中,而前一个字符串将出现在[-2] 索引中。

    将lst[i:] &lt; lst[i: - 1] 更改为lst[-2] &lt; lst[-1]

    还有另一个问题,在第一次迭代中 lst[-2] 不存在,因为只有一个字符串被输入,要摆脱这个 - 将一个输入和 append 它放到列表之前循环开始-

    print("Please enter a string: ")
    s = input()
    lst.append(s)
    finished = False
    while not finished:
        # rest of your code
    

    【讨论】:

      【解决方案2】:

      您可以使用以下方法始终将新项目与现有列表中的最后一个项目进行比较。因此它总是检查下一个项目是否有序

      new_input = input()
      existing_list = ...
      
      if sorted(existing_list[-1], new_input)[-1] == new_input
          existing_list.append(new_input)
          print("Yes")
      else:
          print("No")
      
      

      这样,您不必在检查之前将值输入到列表中

      【讨论】:

        【解决方案3】:

        我对您的代码进行了一些更改。不需要两个输入和一个主列表。代码如下。 假设

        • 假定列表中的项目由单个空格分隔
        • 大写字符和小写字符之间的区别并不重要。如果这不正确并且 ASCII 排序很重要,则从第三行中删除“.lower()”。
        while True:
            print("Please enter a string: ")
            s = input().lower() ## To avoid confusion between sorting ABb Abb and like strings
            if not s: ## If nothing is entered break out of loop
                break
            SplitString = s.split(" ") ##Get elements separated by "single" space into list
            SortedString = " ".join(sorted(SplitString)) ##Sort the list and then join into string
            if s == SortedString: ##If the sorted and joined string is same as original then the string was already sorted when entered
                print("Yes")
            else: ## Else it was not sorted when entered
                print("No")
        

        输出如下

        Please enter a string: 
        AAA AAB ABA ABC
        Yes
        Please enter a string: 
        AAA AAB ABC ABA
        No
        Please enter a string: 
        aaa aab aba abc
        Yes
        Please enter a string: 
        aaa aab abc aba
        No
        Please enter a string: 
        
        

        【讨论】:

          猜你喜欢
          • 2012-01-31
          • 2012-11-19
          • 1970-01-01
          • 2020-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多