python - 如何找出两个随机排列的文本文件中未排序行之间的区别?

标签 python linux bash

我有两个文本文件,其中包含具有相似输入的程序执行结果。 当输入相等时,我想找出这两个文件之间的区别。 每个文件可能包含 1000 次运行的结果,所以我需要找到一个命令,首先检查输入是否相同,然后比较变量的值。 这两个程序总是有相同数量的输入。 但是,输入的数量可以从一组不同的程序中更改,这意味着我有一个 50 个主程序,每个主程序都包含两个我想比较的程序。 例如

//file1.txt
//This is starting at the first line of file1

value in dict:
c: -5493.000000
b: -5493.000000
a: 0.000000
inp_y2: -5493.000000
inp_x2: 0.000000
inp_y1: 0.000000
inp_x1: 0.000000
inp_n: 0.000000

value in dict:
b: -541060888.000000
a: -2147479552.000000
inp_y2: 1571.000000
inp_x2: 541065601.000000
inp_y1: 0.000000
inp_x1: -2147479552.000000
inp_n: 1571.000000


//file2.txt
//This section starts at line 1050

value in dict:
b: -5493.000000
a: 1.000000
inp_y2: -5493.000000
inp_x2: 0.000000
inp_y1: 0.000000
inp_x1: 0.000000
inp_n: 0.000000

value in dict:
b: -541060888.000000
a: -2147479552.000000
inp_y2: 1571.000000
inp_x2: 541065601.000000
inp_y1: 0.000000
inp_x1: -2147479552.000000
inp_n: 1571.000000

所以我期望的是打印:输入的集合和被改变的变量的值

inp_y2: -5493.000000
inp_x2: 0.000000
inp_y1: 0.000000
inp_x1: 0.000000
inp_n: 0.000000

a=0.000000, a=1.000000

我很高兴通过使用例如 numpy 在 bash 或 python 中获得任何解决方案。 笔记: 这是一次运行的唯一结果,在一个文件中我可能有 1000 个“字典中的值:”代表每次运行的开始。

最佳答案

我认为您正在寻找类似于以下功能的东西:

def dict_compare(d1, d2):
    d1_keys = set(d1.keys())
    d2_keys = set(d2.keys())
    intersect_keys = d1_keys.intersection(d2_keys)
    added = d1_keys - d2_keys
    removed = d2_keys - d1_keys
    modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o] != d2[o]}
    same = set(o for o in intersect_keys if d1[o] == d2[o])
    return added, removed, modified, same


x = {
"c": -5493.000000,
"b": -5493.000000,
"a": 0.000000,
"inp_y2": -5493.000000,
"inp_x2": 0.000000,
"inp_y1": 0.000000,
"inp_x1": 0.000000,
"inp_n": 0.000000,
}

y = {
"b": 1.000000,
"a": 0.000000,
"inp_y2": -5493.000000,
"inp_x2": 0.000000,
"inp_y1": 0.000000,
"inp_x1": 0.000000,
"inp_n": 0.000000,
}
added, removed, modified, same = dict_compare(x, y)
print("added: {}, removed: {}, modified: {}, same: {}".format(added, removed, modified, same))

输出:

>>> python3 test.py 
added: {'c'}, removed: set(), modified: {'b': (-5493.0, 1.0)}, same: {'a', 'inp_y1', 'inp_x1', 'inp_n', 'inp_y2', 'inp_x2'}

相关SO答案:https://stackoverflow.com/a/18860653/11502612

编辑:

以下解决方案可根据需要处理文件。

file1.txt 的内容

c: -5493.000000
b: -5493.000000
a: 0.000000
inp_y2: -5493.000000
inp_x2: 0.000000
inp_y1: 0.000000
inp_x1: 0.000000
inp_n: 0.000000

file2.txt 的内容:

b: -5493.000000
a: 1.000000
inp_y2: -5493.000000
inp_x2: 0.000000
inp_y1: 0.000000
inp_x1: 0.000000
inp_n: 0.000000

更改代码的相关部分:

file_1 = {}
file_2 = {}

with open("file1.txt", "r") as opened_file:
    lines = opened_file.readlines()
    for line in lines:
        file_1[line.split(":")[0]] = float(line.split(":")[1].strip())

with open("file2.txt", "r") as opened_file:
    lines = opened_file.readlines()
    for line in lines:
        file_2[line.split(":")[0]] = float(line.split(":")[1].strip())


added, removed, modified, same = dict_compare(file_1, file_2)

输出:

>>> python3 test.py 
added: {'c'}, removed: set(), modified: {'a': (0.0, 1.0)}, same: {'inp_n', 'b', 'inp_x2', 'inp_y1', 'inp_x1', 'inp_y2'}

关于python - 如何找出两个随机排列的文本文件中未排序行之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57356868/

相关文章:

python - 如何在Python中读取unicode文件名?

python - Telegram 机器人 Api/Python : Trying to send voice message via my telegram bot

linux - 无法从 debian 中删除、清除、卸载 mongodb

json - 第 1 行解析错误 : { #networkports "l ----------^ Expecting ' STRING', '}'

c - C代码缩进练习

python - 如何处理有关将 int 应用于包含一项的系列的 FutureWarning?

python - Pandas - 从日期列表中获取每个月的最后一个日期

c++ - 使用 -Wpedantic : "style of line directive is a GCC extension" 编译 .cu 时出现警告

linux - Bash如何从基于dn的ldif类型文件中选择多行

bash - 在 Bash 变量赋值中找不到命令错误