Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建一个程序,如果按字典顺序输入三个单词,则该程序将输出true_Python_String_Python 3.x_Lexicographic - Fatal编程技术网

Python 创建一个程序,如果按字典顺序输入三个单词,则该程序将输出true

Python 创建一个程序,如果按字典顺序输入三个单词,则该程序将输出true,python,string,python-3.x,lexicographic,Python,String,Python 3.x,Lexicographic,我试图创建一个程序,要求用户输入三个单词,如果这些单词是按字典顺序输入的,则打印“True”。例如: Enter first word: chicken Enter second word: fish Enter third word: zebra True 以下是我目前的代码: first = (input('Enter first word: ')) second = (input('Enter second word: ')) third = (input('Enter third

我试图创建一个程序,要求用户输入三个单词,如果这些单词是按字典顺序输入的,则打印“True”。例如:

Enter first word: chicken

Enter second word: fish

Enter third word: zebra

True
以下是我目前的代码:

first = (input('Enter first word: '))
second = (input('Enter second word: '))
third = (input('Enter third word: '))
s = ['a','b','c','d','e','f','g','h',
     'i','j','k','l','m','n','o','p',
     'q','r','s','t','u','v','w','x',
     'y','z','A','B','C','D','E','F',
     'G','H','I','J','K','L','M','N',
     'O','P','Q','R','S','T','U','V',
     'W','Z','Y','Z']
if s.find(first[0]) > s.find(second[0]) and s.find(second[0]) >                                  s.find(third[0]):
    print(True)

如果您处理任意长度的列表,我相信按照其他答案的指示使用对于小列表(带有小字符串)是有好处的,当涉及到较大的列表、较大的字符串和大小写(以及列表可以随机排序的情况)时,更快的方法是使用内置函数和生成器表达式(这应该比方法快)-

#Assuming list is called lst
print(all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1)))
lst = [input("Enter word {}:".format(i)) for i in range(3)] #Change 3 to the number of elements you want to take input from user.
first = (input('Enter first word: '))
second = (input('Enter second word: '))
third = (input('Enter third word: '))
if first <= second <= third:
    print(True)
if first.lower() <= second.lower() <= third.lower():
    print(True)

上述方法的计时结果vs
sorted()
(将
sorted()
的代码修改为不敏感的工作案例)-

或者,如果您只想比较第一个字符(尽管我非常怀疑)-


如果第一个[0]是,列表没有
find
方法。尽管您甚至不必使用列表。
=
)运算符按字典顺序比较序列。此外,Python支持链式比较。我是这样写的:

first = input('Enter first word: ')
second = input('Enter second word: ')
third = input('Enter third word: ')
print(first <= second <= third)
这两种方法都适用于少量单词。如果单词的数量很大,你应该考虑使用短路表达式和生成器表达式:

prev_it, cur_it = iter(words), iter(words)
# Consume first element
next(cur_it)
# Pairwise iteration
print(all(prev <= cur for prev, cur in zip(prev_it, cur_it)))
基于列表的方法示例:

words = [s.lower() for s in input('Enter words (separated by whitespace): ').split()]
将单词存储在a中,然后用检查。您可以通过指定一个键来忽略大小写,该键查看每个单词的小写版本以进行比较:

words = input("Enter three words, separated by spaces: ").split()
print(words == sorted(words, key=str.lower))

你是指字母顺序吗?提示:
string
模块有一个名为
ascii\u letters
的变量。该变量应移到。@curiousdannii-不应移到CR,因为它不是有效的代码。第一个示例非常适合我所寻找的,但是,有没有办法使其中一个单词以大写字母开头时,不会影响其为真或假的影响?添加了一个示例,将所有字符串转换为同一大小写,并进行比较。这样的话,这个案子就无关紧要了。它看起来已经有三根弦了。你甚至都没有把他们列在名单上。这个答案充满了糟糕的编程习惯,我肯定会投反对票。@TigerhawkT3如果它仍然值得投反对票,你还应该指出答案中有什么糟糕的编程习惯。硬编码多个编号变量,例如第一个
第二个
,等等,是一个糟糕的习惯。不将变量放入
列表
元组
或其他序列(即使它们是硬编码的)是另一个问题。这导致了另一个坏习惯,即当您只想检查每个变量是否已排序时,链接每个变量的比较,而函数已经存在。问题是,那些认为这是好代码的人最终会被这些习惯咬到,你我都知道他们的混乱会变成这样的问题。
words = input('Enter words (separated by whitespace): ').split()
print(sorted(words) == words) # True if sorted() didn't re-order anything
prev_it, cur_it = iter(words), iter(words)
# Consume first element
next(cur_it)
# Pairwise iteration
print(all(prev <= cur for prev, cur in zip(prev_it, cur_it)))
print(first.lower() <= second.lower() <= third.lower())
words = [s.lower() for s in input('Enter words (separated by whitespace): ').split()]
words = input("Enter three words, separated by spaces: ").split()
print(words == sorted(words, key=str.lower))