我正在尝试创建一个程序,询问用户三个单词,如果按字典顺序输入单词,则打印'True'。 E.G:
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)
答案 0 :(得分:8)
如果你处理一个任意长度的列表,我相信使用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.
上述方法的时间结果vs 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()
(当它看到第一个{{1}时会短路}})。
此外,事实上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)
答案 1 :(得分:6)
是的,列表没有find
方法。虽然你甚至没有 来使用列表。 <=
(以及>=
)运算符按字典顺序比较序列。此外,Python支持链式比较。以下是我的写作方式:
first = input('Enter first word: ')
second = input('Enter second word: ')
third = input('Enter third word: ')
print(first <= second <= third)
如果超过3个字,您可以将它们收集到列表中,对其进行排序并将其与源列表进行比较。例如:
words = input('Enter words (separated by whitespace): ').split()
print(sorted(words) == words) # True if sorted() didn't re-order anything
对于少量单词,这两种方法都能很好地工作。如果字数很大,您应该考虑使用built-in all
function和生成器表达式的短路解决方案:
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)))
这是第一种解决方案的有效推广。
如果要执行不区分大小写的比较,请在Python 3.3 +中使用str.lower
(或str.casefold
)。
第一个代码段的示例:
print(first.lower() <= second.lower() <= third.lower())
基于列表的方法的示例:
words = [s.lower() for s in input('Enter words (separated by whitespace): ').split()]
答案 2 :(得分:3)