该程序必须打印出8个元素中按字母顺序最后一个名称。
可以通过代码以任何方式输入名称/单词。
我认为我应该在此处使用列表和 in range()
。我有一个想法,即将输入名称的第一个/第二个/第三个/...个字母与先前名称的字母进行比较,然后将其放在列表末尾或前面(取决于比较结果),然后重复这个过程直到下一个名称。最后,程序将打印出列表的最后一个成员。
该程序必须打印出8个元素中按字母顺序最后一个名称。
可以通过代码以任何方式输入名称/单词。
我认为我应该在此处使用列表和 in range()
。我有一个想法,即将输入名称的第一个/第二个/第三个/...个字母与先前名称的字母进行比较,然后将其放在列表末尾或前面(取决于比较结果),然后重复这个过程直到下一个名称。最后,程序将打印出列表的最后一个成员。
Python的字符串比较默认是按字典顺序进行的,所以您应该能够直接调用max
函数:
In [15]: sentence
Out[15]: ['this', 'is', 'a', 'sentence']
In [16]: max(sentence)
Out[16]: 'this'
In [16]: sentence
Out[16]: ['this', 'is', 'a', 'sentence']
In [17]: answer = ''
In [18]: for word in sentence:
....: if word > answer:
....: answer = word
....:
In [19]: print answer
this
或者你可以对你的句子进行排序:
In [20]: sentence
Out[20]: ['this', 'is', 'a', 'sentence']
In [21]: sorted(sentence)[-1]
Out[21]: 'this'
或者,反向排序:
In [25]: sentence
Out[25]: ['this', 'is', 'a', 'sentence']
In [26]: sorted(sentence, reverse=True)[0]
Out[26]: 'this'
def compare(s1, s2):
for i,j in zip(s1, s2):
if ord(i)<ord(j):
return -1
elif ord(i)>ord(j):
return 1
if len(s1)<len(s2):
return -1
elif len(s1)>len(s2):
return 1
else return 0
answer = sentence[0]
for word in sentence[1:]:
if compare(answer, word) == -1:
answer = word
# answer now contains the biggest word in your sentence
str.lower()
:sentence = [word.lower() for word in sentence] # do this before running any of the above algorithms
sort()
方法。strings = ['c', 'b', 'a']
strings.sort()
print strings
['a', 'b', 'c']
如果您想要最后一个,您可以使用max()
方法。
如前面的回答所述,默认情况下字符串比较是按字典顺序进行的,因此可以使用min()
和max()
。为了处理大写和小写单词,可以指定key=str.lower
。例如:
s=['This', 'used', 'to', 'be', 'a', 'Whopping', 'Great', 'sentence']
print min(s), min(s, key=str.lower)
# Great a
print max(s), max(s, key=str.lower)
# used Whopping
locale.collate
的提及。 - abarnertfrom string import capwords
words = ['bear', 'Apple', 'Zebra','horse']
words.sort(key = lambda k : k.lower())
answer = words[-1]
结果:
>>> answer
'Zebra'
>>> words
['Apple', 'bear', 'horse', 'Zebra']
string.capwords(s)
而不是只用 s.lower()
或 s.upper()
? - abarnerts.lower()
和s.upper()
的优点是您不需要导入任何模块(string
),您知道其他优点吗? - Akavalls.lower()
是进行不区分大小写(ASCII)比较的典型方式;使用一个如此鲜为人知以至于他们选择不将其添加为str
方法的函数意味着人们必须思考代码,而不是立即认识到它的作用(自动重构工具根本无法识别它)。你听说过“TOOWTDI”吗?还有一些小优点——lower
也更有可能在不同的Python实现上进行优化,更容易阅读(一个单词告诉你它做什么,而不是两个单词中的一个与此无关),等等。 - abarnertDefine the string: For arguments sake let us say that the string is already predefined.
sentence = "This is the sentence that I need sorted"
Use the split()
method: The split()
method returns a list of "words" from the sentence
string. I use the term "word" here loosely as the method has no conception of "word", it merely separates the sentence
string into a list by parsing it for characters delimited by whitespace and outputs these characters as discrete items in a list. This list is not yet alphabetically ordered.
split_sentence = sentence.split()
Use the sorted
function: The sorted function returns an alphabetically ordered version of the split_sentence
list.
sorted_sentence = sorted(split_sentence)
Print the last element in the list:
print(sorted_sentence[-1])
listName.sort()
这将导致一个按字母顺序排序的列表。
只需使用以下内容:
max(sentence.lower().split())
sort
或max
? - Stuartmax
和sort
的答案不一定会正确处理大小写,因为它们按照字符的数字值进行排序(所以A
<B
,但a
>B
)。 - Brendan Long