python - 创建一个程序,如果按字典顺序输入三个单词则打印 true

标签 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 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)

最佳答案

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

#Assuming list is called lst
print(all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1)))

请注意,上面的代码最终会调用 str.lower()在每个字符串上(第一个和最后一个除外)两次。除非你的字符串非常大,否则这应该没问题。

如果您的字符串与列表的长度相比确实非常大,您可以在执行 all() 之前创建另一个临时列表。以小写形式存储所有字符串。然后在该列表上运行相同的逻辑。

您可以使用列表理解创建列表(通过获取用户的输入),示例 -

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.

上述方法的计时结果与 sorted()(修改后的 sorted() 代码不区分大小写)-

In [5]: lst = ['{:0>7}'.format(i) for i in range(1000000)]

In [6]: lst == sorted(lst,key=str.lower)
Out[6]: True

In [7]: %timeit lst == sorted(lst,key=str.lower)
1 loops, best of 3: 204 ms per loop

In [8]: %timeit all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1))
1 loops, best of 3: 439 ms per loop

In [11]: lst = ['{:0>7}'.format(random.randint(1,10000)) for i in range(1000000)]

In [12]: %timeit lst == sorted(lst,key=str.lower)
1 loops, best of 3: 1.08 s per loop

In [13]: %timeit all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1))
The slowest run took 6.20 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 2.89 µs per loop

结果-

对于应该返回 True 的情况(即已经排序的列表),使用 sorted()all() 快得多,因为 sorted() 适用于大部分排序的列表。

对于随机情况,all()sorted() 效果更好,因为 all() 的短路特性>(它会在看到第一个 False 时短路)。

此外,sorted() 会在内存中创建一个临时(排序列表)(用于比较),而 all() 则不需要(这个事实确实归因于我们在上面看到的时间)。


较早的直接回答(并且只适用于这个问题)你可以简单地直接比较字符串,你不需要另一个字符串/字母列表。示例 -

first = (input('Enter first word: '))
second = (input('Enter second word: '))
third = (input('Enter third word: '))
if first <= second <= third:
    print(True)

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

if first[0] <= second[0] <= third[0]:
    print(True)

要不区分大小写地比较字符串,可以在比较之前将所有字符串转换为小写。示例 -

if first.lower() <= second.lower() <= third.lower():
    print(True)

或者更简单的——

print(first.lower() <= second.lower() <= third.lower())

关于python - 创建一个程序,如果按字典顺序输入三个单词则打印 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33183399/

相关文章:

python - 根据某些标准查找列表的共同元素?

python - 排序功能。解释

c# - 如何使用正则表达式验证带有空格的逗号分隔字符串

c - C 中的 if 语句不稳定

python - 接受 int 或 int 元组作为 python 函数参数

python - 使用较小的 N 维数组按列对 N 维 numpy 数组进行排序

Android Matcher 和 Pattern 从链接中切出

javascript - 执行使用 python smtplib 发送的电子邮件的 Javascript

python - 使用 Python 的简单 'chat' UDP 客户端和服务器,不能使用 `str` 作为 `send()` 有效载荷

python - 将系数正则化添加到 Statsmodels(或 Patsy)