Problem when calling dictionary variables. TypeError: tuple indices must be integers or slices, not str
Problem when calling dictionary variables. TypeError: tuple indices must be integers or slices, not str
我想打印 result1 的 variable_1 和 result2 的 variable_3。我想尝试并继续使用类似的语法,我可以在其中指定 result1 和 variable_1 的调用。 我想打印并调用这两个条件。
我收到以下错误:TypeError: tuple indices must be integers or slices, not str
我该如何解决?
def a():
result1 = {}
if 2 > 1:
print("yes 1")
result1['variable_1'] = 2+2
result1['variable_11'] = 'yes11'
else:
print("no 1")
result1['variable_2'] = 'no1'
result2 = {}
if 3 < 2:
print("yes 2")
result2['variable_3'] = 'yes2'
else:
print("no 2")
result2['variable_4'] = 'no2'
return result1, result2
result1 = a()
result2 = a()
print(result1['variable_1'])
print(result2['variable_3'])
您给出的代码有 a()
returns 两个值。
所以您的代码的作用如下:
result1 = (result1, result2)
result2 = (result1, result2)
其中括号内的值是基于
a()
.
中的变量
你能做什么:
def a():
result1 = {}
if 2 > 1:
print("yes 1")
result1['variable_1'] = 2+2
result1['variable_11'] = 'yes11'
else:
print("no 1")
result1['variable_2'] = 'no1'
result2 = {}
if 3 < 2:
print("yes 2")
result2['variable_3'] = 'yes2'
else:
print("no 2")
result2['variable_4'] = 'no2'
return result1, result2
result1, result2 = a()
print(result1['variable_1'])
print(result2['variable_3'])
减少混淆的更好方法:
def a():
r1 = {"variable_1": None,
"variable_11": None,
"variable_2": None}
if 2 > 1:
print("yes 1")
r1['variable_1'] = 2+2
r1['variable_11'] = 'yes11'
else:
print("no 1")
r1['variable_2'] = 'no1'
r2 = {"variable_3": None,
"variable_4": None}
if 3 < 2:
print("yes 2")
r2['variable_3'] = 'yes2'
else:
print("no 2")
r2['variable_4'] = 'no2'
return r1, r2
result1, result2 = a()
print(result1['variable_1'])
print(result2['variable_3'])
这个问题也是因为你没有默认
variable_3
的值,因为在 result2
中,3 < 2
是 False
,因此 r2['variable_4']
已定义,但 r2['variable_3']
未定义。
我将默认值设为None
;但您可以将其设置为任何您想要的。
也就是说,虽然上面的代码有效,但我不完全确定您要做什么。
您正在将两个不同的变量分配给同一事物:一个元组。你真正想要的是元组拆包。所以不要这样做:
result1 = a()
result2 = a()
做:
result1, result2 = a()
你这样做是因为 a()
returns 2 个不同的值,你想为每个值分配一个变量。相当于做
result1 = a()[0]
result2 = a()[1]
另请注意,条件 3 < 2
永远不会为真,因此永远不会分配该值。
我想打印 result1 的 variable_1 和 result2 的 variable_3。我想尝试并继续使用类似的语法,我可以在其中指定 result1 和 variable_1 的调用。 我想打印并调用这两个条件。
我收到以下错误:TypeError: tuple indices must be integers or slices, not str
我该如何解决?
def a():
result1 = {}
if 2 > 1:
print("yes 1")
result1['variable_1'] = 2+2
result1['variable_11'] = 'yes11'
else:
print("no 1")
result1['variable_2'] = 'no1'
result2 = {}
if 3 < 2:
print("yes 2")
result2['variable_3'] = 'yes2'
else:
print("no 2")
result2['variable_4'] = 'no2'
return result1, result2
result1 = a()
result2 = a()
print(result1['variable_1'])
print(result2['variable_3'])
您给出的代码有 a()
returns 两个值。
所以您的代码的作用如下:
result1 = (result1, result2)
result2 = (result1, result2)
其中括号内的值是基于
a()
.
你能做什么:
def a():
result1 = {}
if 2 > 1:
print("yes 1")
result1['variable_1'] = 2+2
result1['variable_11'] = 'yes11'
else:
print("no 1")
result1['variable_2'] = 'no1'
result2 = {}
if 3 < 2:
print("yes 2")
result2['variable_3'] = 'yes2'
else:
print("no 2")
result2['variable_4'] = 'no2'
return result1, result2
result1, result2 = a()
print(result1['variable_1'])
print(result2['variable_3'])
减少混淆的更好方法:
def a():
r1 = {"variable_1": None,
"variable_11": None,
"variable_2": None}
if 2 > 1:
print("yes 1")
r1['variable_1'] = 2+2
r1['variable_11'] = 'yes11'
else:
print("no 1")
r1['variable_2'] = 'no1'
r2 = {"variable_3": None,
"variable_4": None}
if 3 < 2:
print("yes 2")
r2['variable_3'] = 'yes2'
else:
print("no 2")
r2['variable_4'] = 'no2'
return r1, r2
result1, result2 = a()
print(result1['variable_1'])
print(result2['variable_3'])
这个问题也是因为你没有默认
variable_3
的值,因为在 result2
中,3 < 2
是 False
,因此 r2['variable_4']
已定义,但 r2['variable_3']
未定义。
我将默认值设为None
;但您可以将其设置为任何您想要的。
也就是说,虽然上面的代码有效,但我不完全确定您要做什么。
您正在将两个不同的变量分配给同一事物:一个元组。你真正想要的是元组拆包。所以不要这样做:
result1 = a()
result2 = a()
做:
result1, result2 = a()
你这样做是因为 a()
returns 2 个不同的值,你想为每个值分配一个变量。相当于做
result1 = a()[0]
result2 = a()[1]
另请注意,条件 3 < 2
永远不会为真,因此永远不会分配该值。