python - 排序区分大小写

标签 python python-2.7

我有一个文本文件(a.txt)有很多英文单词,我想在其他文本文件(inOrder.txt)上重写该文件

我现在得到的代码是区分大小写的,所以它将首先列出以大写字母开头的单词 A..B..C.. D..until..Z 然后以小写字母 a.. b.. c.. d.. .....z,无论首字母大小写如何,我都需要它来订购它们。

这是我的代码:

f= open('a.txt', 'r+')
data = [line.strip() for line in f.readlines()]
print sorted(data)

with open("inOrder.txt", "w") as f: f.write("\n".join(line for line in sorted(data) if line.strip()))

除此之外,我想将结果写在不止一列上,它们大约有 8000(字/行),例如我需要每列包含 1000 个字,所以我最终可以得到 8 列。

我正在使用 python 2.7.8

最佳答案

python sorted方法接受 key 关键字参数。

sorted(iterable, key=str.lower)

它对可迭代的元素进行排序,不管它们的大小写。 Key 必须是一个 callable 对象。

关于python - 排序区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28714528/

相关文章:

python - 动态选择要使用的 API

python - 使用 pathlib 重命名文件扩展名(python 3)

Python - 输出分数而不是小数

python - Django ORM - 不同表中行的部分匹配

Python将字符串放入字典

python - 欧拉计划 #17 的意外结果(Python 3 与 Python 2.7)

线程 : can't start new thread 中的 Python 异常

python - 在 python 中重用已编译的正则表达式

python - 导入错误: No module named enum

python - 如何使用请求模块检测站点检查何时重定向到另一个页面?