首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何给出一个条件,如果两个十六进制数之间的差是2,那么连接两行?

如何给出一个条件,如果两个十六进制数之间的差是2,那么连接两行?
EN

Stack Overflow用户
提问于 2022-10-20 05:17:22
回答 2查看 69关注 0票数 1

我有一个包含两列的文件(如abc.txt )

代码语言:javascript
运行
复制
000000008b8c5200        af dg sd jh g1 43 66 23 67
000000008b8c5220        bc bi ub cb ue qi hd 16 72
0000000056fb2620        ad ag sj ha bn bc bh 53 69
0000000056fb2640        ak bh jg bf re 34 16 8g ff
0000000045ab4630        sg fj g3 6g dh w7 28 g7 dg
0000000045ab4650        jb sh jd b7 us vy ys du 89

这里,我需要将第二行第二列与第一行第一列连接起来,如下所示:

代码语言:javascript
运行
复制
bcbiubcbueqihd1672afdgsdjhg143662367

连接条件:

只有当第2行、第1列和第1行之间(十六进制)差为20时,第1列才是20。

代码语言:javascript
运行
复制
000000008b8c5220 - 000000008b8c5200 = 20.
0000000056fb2640 - 0000000056fb2620 = 20.
0000000045ab4650 - 0000000045ab4630 = 20.

对于即将出现的行和列,类似地。将结果写入具有第一行和连接数据的文件,如下所示:

代码语言:javascript
运行
复制
000000008b8c5200 bcbiubcbueqihd1672afdgsdjhg143662367
0000000056fb2620 akbhjgbfre34168gffadagsjhabnbcbh5369
0000000045ab4630 jbshjdb7usvyysdu89sgfjg36gdhw728g7dg

我该怎么做?

EN

Stack Overflow用户

回答已采纳

发布于 2022-10-20 06:08:32

以下是你如何做到这一点:

代码语言:javascript
运行
复制
with open('input.txt', 'r') as f, open('output.txt', 'w') as g:
    out = []
    lines = [elem.strip().split() for elem in f] # lines will be list of lists, each list with 2 elements (-> 2 columns)
    for line_x, line_y in zip(lines[::2], lines[1::2]): # use zip and steps of 2 to iterate like: 1&2, 3&4, 5&6...
        num_x = int(line_x[0], base=16) # convert hex to int
        num_y = int(line_y[0], base=16)
        print(num_x, num_y, num_y - num_x)
        if num_y - num_x == 32:
            # if difference is 20, build string seperated by tab with the hex number and the concatenated data
            new_row = '\t'.join([line_x[0], line_y[1] + line_y[1]])
            out.append(new_row)
    g.write('\n'.join(out)) # write all new rows to output file

对于提供的示例,它打印:

代码语言:javascript
运行
复制
2341229056 2341229088 32
1459299872 1459299904 32
1168852528 1168852560 32

因为没有行差为20,所以输出文件中将没有数据。

对于您更改的输入,可以这样做:更新

代码语言:javascript
运行
复制
with open('input.txt', 'r') as f, open('output.txt', 'w') as g:
    out = []
    lines = [elem.strip().split() for elem in f] # lines will be list of lists
    print(lines[0]) # this is how each line looks
    # ['000000008b8c5200', 'af', 'dg', 'sd', 'jh', 'g1', '43', '66', '23', '67']
    
    for a, b in zip(lines[::2], lines[1::2]): # use zip and steps of 2 to iterate like: 1&2, 3&4, 5&6...
        num_a, remaining_a = a[0], ''.join(a[1:])
        num_b, remaining_b = b[0], ''.join(b[1:])
        
        if int(num_b, base=16) - int(num_a, base=16) == 20: # convert hex string to int and build difference
            # if difference is 20, build a tuple with the number as 1st element and concatenated data as 2nd element
            new_row = '\t'.join([num_a, remaining_b + remaining_a])
            out.append(new_row)
            
    g.write('\n'.join(out)) # write row to new file
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74134756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档