python-3.x - 在文本文件中按升序排列
问题描述
所以我有一个看起来像这样的文本文件:
07,12,9201
07,12,9201
06,18,9209
06,18,9209
06,19,9209
06,19,9209
07,11,9201
我首先要删除所有重复的行,然后按升序对第 1 列进行排序,然后按升序对第 2 列进行排序,因为第 1 列仍然是升序。输出:
06,18,9209
06,19,9209
07,11,9201
07,12,9201
到目前为止,我已经尝试过:
with open('abc.txt') as f:
lines = [line.split(' ') for line in f]
考虑另一个例子:
00,0,6098
00,1,6098
00,3,6098
00,4,6094
00,5,6094
00,6,6094
00,7,6094
00,8,6094
00,9,6498
00,2,6098
00,20,6102
00,21,6087
00,22,6087
00,23,6087
00,3,6098
00,4,6094
00,5,6094
00,6,6094
00,7,6094
00,8,6094
00,9,6498
此文件的输出应为:
00,0,6098
00,1,6098
00,2,6098
00,3,6098
00,4,6094
00,5,6094
00,6,6094
00,7,6094
00,8,6094
00,9,6498
00,20,6102
00,21,6087
00,22,6087
00,23,6087
解决方案
您可以执行以下操作。
from itertools import groupby, chain
from collections import OrderedDict
input_file = 'input_file.txt'
# Collecting lines
lines = [tuple(line.strip().split(',')) for line in open(input_file)]
# Removing dups and Sorting by first column
sorted_lines = sorted(set(lines), key=lambda x: int(x[0]))
# Grouping and ordering by second column
result = OrderedDict()
for k, g in groupby(sorted_lines, key=lambda x: x[0]):
result[k] = sorted(g, key = lambda x : int(x[1]))
print(result)
for v in chain(*result.values()):
print(','.join(v))
输出 1:
06,18,9209
06,19,9209
07,11,9201
07,12,9201
输出 2:
00,0,6098
00,1,6098
00,2,6098
00,3,6098
00,4,6094
00,5,6094
00,6,6094
00,7,6094
00,8,6094
00,9,6498
00,20,6102
00,21,6087
00,22,6087
00,23,6087
推荐阅读
- python-3.x - 如何通过其方法获取类对象?,尝试使用 dir() 和 __class__,但它输出
- json - 需要基于类的编程选择创建自定义模式
- python - 通过列表循环错误帮助进行迭代 - Yfinance
- javascript - Html.CheckBoxFor 使用 Jquery 查找状态
- regex - PowerShell - 将正则表达式与线程作业输出匹配但不填充 $matches 变量
- python - 如何在输出到终端时显示带有正确换行符的 .txt 文件中的文本
- ios - 将 HealthKit 数据(氧饱和度)传递给 iOS 上的小部件
- python - 如何让一个模块中的函数等待单独模块中的另一个函数(成功)开始?
- javascript - 如何将文件内容转换为字符串?
- xamarin - 自定义控件中的焦点条目