如何将字典列表与另一个字典列表相匹配,其中键是不同的?

时间:2017-07-05 23:54:30

标签: python dictionary

我遇到了匹配两个词典列表的问题。在我试图匹配的代码下面。

listDict1= [
    '{"weightMax":150, "weightMin":100, "date":"2017-07-05 12:16:06"}',
    '{"weightMax":150, "weightMin":100, "date":"2017-07-05 12:17:06"}',
    '{"weightMax":0, "weightMin":0, "date":"0"}']

listDict2= [
    '{"Weight":"138.0","unit":"Kg",
      "measurementTime":"2017-07-05 12:15:06",
      "receiptTime":"2017-06-30T15:16:09.009-07:00",
      "date":1498860969000,"model":"2011",
      "manufacturer":"CONTINUA","serialnumber":"SN_WEIGHT082"}',
    '{"Weight":"138.0","unit":"Kg",
      "measurementTime":"2017-07-05 12:16:06",
      "receiptTime":"2017-06-30T15:16:09.009-07:00",
      "date":1498860969000,"model":"2011",
      "manufacturer":"CONTINUA","serialnumber":"SN_WEIGHT082"}',
    '{"Weight":"188.0","unit":"Kg",
      "measurementTime":"2017-07-05 12:17:06",
      "receiptTime":"2017-06-30T15:16:09.009-07:00",
      "date":1498860969000,"model":"2011",
      "manufacturer":"CONTINUA","serialnumber":"SN_WEIGHT082"}']

我需要将listDict1 [' date']值与listDict2匹配[' measurementTime']

我尝试了下面的代码,但它按照列表索引匹配

def matchValValues(listDict1, listDict2):
    val1 = []
    rpm = []


    for i in range(len(listDict1)):
        j = dict((k, v) for k, v in ast.literal_eval(listDict1[i]).iteritems())
        k = dict((k, v) for k, v in ast.literal_eval(listDict2[i]).iteritems())
        #for i in j.keys():
        val = j['date']
        we = k['measurementTime']
        if val in we :
            val2 = float(k['Weight'])
            max=  float(j['weightMax'])
            min = float(j['weightMin'])
            print "True"
            if val2 <= max and val2 >= min:
                print "pass"
                val1.append(str(j['weightMax'])+'|'+str(j['weightMin']))
                rpm.append(k['Weight'])
            else:
                val1.append(j.copy())
                rpm.append(k.copy())
        else:
            val1.append(j.copy())
            rpm.append(k.copy())
    print val1  
    print rpm

预期产量: 如果匹配,则仅以rpm为单位追加weight和measurementTime值 如果匹配,则仅在val1中附加weightmax,weightmin和date值 否则在rpm和val1对应的地方追加整个不匹配的dict。

当前输出:

[{'date': '2017-07-05 12:16:06', 'weightMax': 150, 'weightMin': 100},
 {'date': '2017-07-05 12:17:06', 'weightMax': 150, 'weightMin': 100},
 {'date': '0', 'weightMax': 0, 'weightMin': 0}]

[{'Weight': '138.0', 'measurementTime': '2017-07-05 12:15:06',
  'serialnumber': 'SN_WEIGHT082', 'date': 1498860969000L,
  'model': '2011', 'receiptTime': '2017-06-30T15:16:09.009-07:00',
  'unit': 'Kg', 'manufacturer': 'CONTINUA'},
 {'Weight': '138.0', 'measurementTime': '2017-07-05 12:15:06',
  'serialnumber': 'SN_WEIGHT082', 'date': 1498860969000L,
  'model': '2011', 'receiptTime': '2017-06-30T15:16:09.009-07:00',
  'unit': 'Kg', 'manufacturer': 'CONTINUA'},
 {'Weight': '188.0', 'measurementTime': '2017-07-05 12:16:06',
  'serialnumber': 'SN_WEIGHT082', 'date': 1498860969000L,
  'model': '2011', 'receiptTime': '2017-06-30T15:16:09.009-07:00',
  'unit': 'Kg', 'manufacturer': 'CONTINUA'}]

1 个答案:

答案 0 :(得分:0)

它符合列表索引,因为这正是你告诉它要做的。计算机在这方面很烦人。

不要使用索引来选择所需的listDict2条目。而是从listIndex1[i]获取日期并搜索listDict2[<all>]['measurementTime']以获取该值。如果列表很大,您可能需要构建一个<time stamp> : index的反向字典,以使您的工作更轻松。

这会让你感动吗?