【问题标题】:Comparing dictionary list. TypeError: tuple indices must be integers or slices, not str比较字典列表。 TypeError:元组索引必须是整数或切片,而不是 str
【发布时间】:2021-06-19 13:44:14
【问题描述】:

我正在尝试以字典格式比较两组数据(file1、file2)。当使用 example 1(见下文)进行比较时,代码可以工作,但是当我添加更多数据时(example 2)我得到一个错误,因为 tuple indices must be integer或切片。我很难理解这一点,因为我正在比较两组字典而不是使用整数的列表来比较使用索引中的整数而不是字典中的键名。

#Dict 列表示例 1:使用此示例,它可以工作

file1= {'name': 'Phill', 'age': 42}
file2= {'name': 'Phill', 'age': 22}

#Dict 列表示例 2:使用此示例,它不起作用

file1= {'name': 'Phill', 'age': 42},{'name': 'Phill', 'age': 22}
file2= {'name': 'Phill', 'age': 22},{'name': 'Phill', 'age': 52}

#Function with two args

    def diffValue (file1,file2) :
        

    for newAge in file1,file2 :

        if file1 [ 'age' ] == file2 [ 'age' ] :
            # if no difference found in both files within the age field
            print ( "No difference found" )

        else :

            # the age is different. return values name,age where ever there is a difference from file1 only
            return newAge

预期结果:

Return {'name': 'Phill', 'age': 42},{'name': 'Phill', 'age': 22}

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    我认为你打算这样做:

    file1 = {'name': 'Phill', 'age': 42}, {'name': 'Phill', 'age': 22}
    file2 = {'name': 'Phill', 'age': 22}, {'name': 'Phill', 'age': 52}
    
    def diff_value(f1, f2):
        # Create a list to store the differences
        diffs = []
    
        # Iterate for every element in parallel
        for v1, v2 in zip(f1, f2):
            if v1['age'] == v2['age']:
                # If no difference found in both files within the age field
                print('No difference found')
            
            else:
                # The age is different, append v1 to `diffs`
                diffs.append(v1)
        
        return diffs
        
    print(diff_value(file1, file2))
    # Output: [{'name': 'Phill', 'age': 42}, {'name': 'Phill', 'age': 22}]
    

    【讨论】:

      【解决方案2】:
      >>> file1= {'name': 'Phill', 'age': 42},{'name': 'Phill', 'age': 22}
      >>> file1
      ({'name': 'Phill', 'age': 42}, {'name': 'Phill', 'age': 22})
      >>> type(file1)
      <class 'tuple'>
      

      file1 是元组,这就是它给出错误的原因

      使其与您的功能一起使用的一种方法是:

      def diffValue (file1,file2) :
          for newAge in file1,file2 :
              if file1 [ 'age' ] == file2 [ 'age' ] :
                  # if no difference found in both files within the age field
                  print ( "No difference found" )
              else :
                  # the age is different. return values name,age where ever there is a difference from file1 only
                  return newAge
      file1= [{'name': 'Phill', 'age': 42},{'name': 'Phill', 'age': 22}]
      file2= [{'name': 'Phill', 'age': 22},{'name': 'Phill', 'age': 52}]
      
      a=""
      for i in range(len(file1)):
          a+=str(diffValue(file1[i],file2[i]))+","
      a=a[:-1]
      print(a)
      

      【讨论】:

        猜你喜欢
        • 2017-04-12
        • 2017-03-11
        • 2020-07-11
        • 2022-10-04
        • 2015-12-09
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多