Loading... ## 题目 ## 请对给出的一系列英文单词按首字母进行排序,并输出排序后的结果。 输入说明:数字N,表明单词数,接下来是N个英文单词; 输出说明:按字母序的排序结果。 输入样例:6 Apple banana Zebra Tuesday moon CAN 输出样例:Apple banana CAN moon Tuesday Zebra ## 代码 ## ```python n = int(input()) word = input() word = word.split(" ") # word.sort(key = str.lower) word = sorted(word, key = str.lower) for i in range(n): print(word[i]) ``` ## 笔记 ## a = [5,2,3] a.sort() 则是对a本身,进行排序,输出 [2,3,5] `sorted`是新建一个列表,存储排序之后的数据 b = a.sorted() [2,3,5] `str.lower()`将可将字符串小写化,`key = str.lower` 则将word中所有的字符串,转换为小写再进行排序 ### 字符串的常用方法:### [Python len()方法 - 获取字符串长度][1] [Python ljust()方法 - 指定字符串长度][2] [Python lower()方法 - 字符串小写][3] [Python upper()方法 - 字符串大写][4] [Python lstrip()方法 - 删除字符串左侧空格或指定字符][5] [Python maketrans()方法 - ][6] [Python max()方法 - 获取最大字符][7] [Python min()方法 - 获取最小字符][8] [更多......][9] [1]: https://www.runoob.com/python/att-string-len.html [2]: https://www.runoob.com/python/att-string-ljust.html [3]: https://www.runoob.com/python/att-string-lower.html [4]: https://www.runoob.com/python/att-string-upper.html [5]: https://www.runoob.com/python/att-string-lstrip.html [6]: https://www.runoob.com/python/att-string-maketrans.html [7]: https://www.runoob.com/python/att-string-max.html [8]: https://www.runoob.com/python/att-string-min.html [9]: https://www.runoob.com/?s=python%20%E5%AD%97%E7%AC%A6 Last modification:November 17th, 2019 at 08:52 pm © 允许规范转载