如何检查输入的字符串列表是否按字母顺序排列?
How to check if list of strings entered are in alphabetical order?
我有一个程序,它从用户输入中获取一系列字符串,如果输入的字符串按字母顺序排列,则打印“是”,否则打印“否”。该程序以用户输入空输入结束。当我指定它应该有的输入数量时,我可以这样做,例如 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
是否有一种简单的方法可以在不偏离上述预期结构的情况下实现这一目标?
lst
是一个字符串列表,切片语法将返回一个字符串列表。但是你想要一个字符串。由于您要追加 到列表,最新追加的字符串将出现在[-1]
索引中,而前一个将出现在[-2]
索引中。
将lst[i:] < lst[i: - 1]
更改为lst[-2] < lst[-1]
但是还有另一个问题,在第一次迭代中 lst[-2]
不存在,因为只有一个字符串被输入,为了摆脱这个 - 接受一个输入和 append
在循环开始之前将其添加到列表中-
print("Please enter a string: ")
s = input()
lst.append(s)
finished = False
while not finished:
# rest of your code
您可以使用以下方法始终将新项目与现有列表中的最后一项进行比较。因此它总是检查下一个项目是否有序
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")
这样一来,您不必在检查前将值输入到列表中
我对你的代码做了一些修改。不需要两个输入和一个主列表。代码如下。
假设
- 假定列表中的项目由单个 space
分隔
- 大写字符和小写字符之间的区别并不重要。如果这不是真的并且 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:
我有一个程序,它从用户输入中获取一系列字符串,如果输入的字符串按字母顺序排列,则打印“是”,否则打印“否”。该程序以用户输入空输入结束。当我指定它应该有的输入数量时,我可以这样做,例如 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
是否有一种简单的方法可以在不偏离上述预期结构的情况下实现这一目标?
lst
是一个字符串列表,切片语法将返回一个字符串列表。但是你想要一个字符串。由于您要追加 到列表,最新追加的字符串将出现在[-1]
索引中,而前一个将出现在[-2]
索引中。
将lst[i:] < lst[i: - 1]
更改为lst[-2] < lst[-1]
但是还有另一个问题,在第一次迭代中 lst[-2]
不存在,因为只有一个字符串被输入,为了摆脱这个 - 接受一个输入和 append
在循环开始之前将其添加到列表中-
print("Please enter a string: ")
s = input()
lst.append(s)
finished = False
while not finished:
# rest of your code
您可以使用以下方法始终将新项目与现有列表中的最后一项进行比较。因此它总是检查下一个项目是否有序
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")
这样一来,您不必在检查前将值输入到列表中
我对你的代码做了一些修改。不需要两个输入和一个主列表。代码如下。 假设
- 假定列表中的项目由单个 space 分隔
- 大写字符和小写字符之间的区别并不重要。如果这不是真的并且 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: