【发布时间】: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