python-嵌套数组的对比(数组嵌套字典,再嵌套数组..)

需求是对两个嵌套数字进行对比,判断两个数组的内容是否是一样的。

数据长这样

[{'type': 4, 'pos': [403, 323, 563, 352], 'content': '0'}, {'type': 4, 'pos': [492, 572, 609, 630], 'content': '0'}, {'type': 4, 'pos': [412, 740, 532, 798], 'content': '0'}, {'type': 4, 'pos': [112, 807, 178, 865], 'content': '0'}, {'type': 4, 'pos': [82, 1060, 1107, 1432], 'content': '1'}, {'type': 3, 'pos': [82, 1060, 1107, 1432], 'content': '-2'}, {'type': 7, 'pos': [], 'content': '09.07'}, {'type': 2, 'pos': [], 'content': '2'}]
[{'type': 4, 'pos': [112, 807, 178, 865], 'content': '0'}, {'type': 4, 'pos': [82, 1060, 1107, 1432], 'content': '1'}, {'type': 3, 'pos': [82, 1060, 1107, 1432], 'content': '-2'}, {'type': 4, 'pos': [403, 323, 563, 352], 'content': '0'}, {'type': 4, 'pos': [492, 572, 609, 630], 'content': '0'}, {'type': 4, 'pos': [412, 740, 532, 798], 'content': '0'}, {'type': 7, 'pos': [], 'content': '09.07'}, {'type': 2, 'pos': [], 'content': '2'}]

大概有以下几种思路

  • 排序+比较
  • 直接比较(想到两种方法,如果有其他方法,欢迎补充)
    • 使用set
    • 使用in

三种方法都试了:

  1. 本来想排序来着,但里面的嵌套层数太多,无法排序
  2. 可以用set进行比较,set(a)^set(b),但因为里面元素是dict,而dict不支持哈希,报错了。
  3. 后来用了in
    strA = response.json["data"]["answerSheets"]["30371513"]["pages"]["1"]
    strB = [{"type":4,"pos":[112,807,178,865],"content":"0"},{"type":4,"pos":[82,1060,1107,1432],"content":"1"},{"type":3,"pos":[82,1060,1107,1432],"content":"-2"},{"type":4,"pos":[403,323,563,352],"content":"0"},{"type":4,"pos":[492,572,609,630],"content":"0"},{"type":4,"pos":[412,740,532,798],"content":"0"},{"type":7,"pos":[],"content":"09.07"},{"type":2,"pos":[],"content":"2"}]
    res = True
    diff = []
    for i in range(0, len(strA)):
        if(strA[i] not in strB):
            diff.append(strA[i])
            res = False
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值