gpt4 book ai didi

python - 使用python比较文本行(多个列表)

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:32 25 4
gpt4 key购买 nike

我有包含以下行的文本文件:

Cycle 0 DUT 2 Bad Block : 2,4,6,7,8,10,12,14,16,18,20,22,24,26,28
Cycle 0 DUT 3 Bad Block : 4,6,8,10,12,14,16,18,20,22,24,26
Cycle 0 DUT 4 Bad Block : 4,6,8,10,12,14,16,18,20,22,24,26
Cycle 1 DUT 2 Bad Block : 2,4,6,7,8,10,12,14,16,18,20,22,24,26,28
Cycle 1 DUT 3 Bad Block : 4,6,8,10,12,14,16,18,20,22,24,26,28,30,32

我想将 Cycle 0 DUT 2 文本行(冒号后的数字用逗号分隔)与 Cycle 1 DUT 2 文本行(冒号后的数字用逗号分隔)进行比较逗号)并获取差异,然后将 Cycle 0 DUT 3 文本行与 Cycle 1 DUT 3 文本行进行比较,并获取差异或唯一值。

最佳答案

我猜您想将内容键入到 DUT 数字:

import re
dut_data = {}

cycle_dut = re.compile('^Cycle\s+(\d)\s+DUT\s+(\d)\s+Bad Block\s*:\s*(.*)$')

with open(inputfile, 'r') as infile:
for line in infile:
match = cycle_dut.search(line)
if match:
cycle, dut, data = match.groups()
data = [int(v) for v in data.split(',')]
if cycle == '0':
# Store cycle 0 DUT values keyed on the DUT number
dut_data[dut] = data
else:
# Compare against cycle 0 data, if the same DUT number was present
cycle_0_data = dut_data.get(dut)
if cycle_0_data is not None:
# compare cycle_0_data and data here
print 'DUT {} differences: {}'.format(dut, ','.join([str(v) for v in sorted(set(cycle_0_data).symmetric_difference(data))]))

我使用了快速设置差异来打印差异,这可能需要改进。

对于您的示例数据,将打印:

DUT 2 differences: 
DUT 3 differences: 28,30,32

关于python - 使用python比较文本行(多个列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15936118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com