修改字典中的值时出现类型错误
Getting type error when modifying the values in a dictionary
我制作了以下词典:
client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}
我想:从用户那里获取输入,显示客户端的值,如果可以,则保留字典的当前状态,否则,用户可以更改给定客户端的值。为此,我做了以下操作:
x = client_dict[input('Enter the client name:\n')]
print(x)
y = input('if ok enter y otherwise enter n:\n')
if y =='n':
lst = []
for i in range(len(x)):
x[i] = input('enter the correct header:\n')
lst.append(x[i])
client_dict[x] = lst
else:
pass
假设我在第一个输入中输入 client 1
然后输入 n
意味着我想更改值。然后,算法两次要求我输入所需的 header(因为客户端 1 有两个值),第一个 header 我写 hello
,第二个我写 world
.阵容如下:
Enter the client name:
client 1
['ABC', 'EFG']
if ok enter y otherwise enter n:
n
enter the correct header:
hello
enter the correct header:
world
我现在可以查看我的 client_dict
修改为:
{'client 1': ['hello', 'world'],
'client 2': ['MNO', 'XYZ'],
'client 3': ['ZZZ']}
这意味着代码做我想要的,但是当条件语句中的过程结束时,我也得到以下错误:
TypeError: unhashable type: 'list'
来自于此:client_dict[x] = lst
。所以我想知道我做错了什么?尽管代码有效,但似乎在重写字典时出现了一些问题?
我认为您不小心将值赋给了 client_dict 中的键。比如,您试图说一个值,即列表,是它出错所在行的新条目中的键。你可能想做这样的事情:
client_dict["client1"] = x
但用代表该客户名称的变量替换“client1”。你可能错误地认为 x 是你的客户的名字(也就是 key),但它实际上等于这个字典条目的 value ,因为这一行:
x = client_dict[input('Enter the client name:\n')]
这就是说“当我进入字典“client_dict”并访问键(input() 调用的结果)所在的位置时,将 x 分配给结果值”
写出你的字典中 kvp 的含义可能会有所帮助:
键应该是:字符串
值应为:列表
然后,检查您的代码并考虑所有内容的类型。这是 Python 的一个问题,因为它很容易混淆每个变量应该表示的类型,因为它是动态类型的
我更改了你的代码,试试这个:
client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}
x = input('Enter the client name:\n')
print(client_dict[x])
y = input('if ok enter y otherwise enter n:\n')
if y == 'n':
for i in range(len(client_dict[x])):
client_dict[x][i] = input('enter the correct header:\n')
else:
pass
抱歉,请立即尝试。 已编辑
我制作了以下词典:
client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}
我想:从用户那里获取输入,显示客户端的值,如果可以,则保留字典的当前状态,否则,用户可以更改给定客户端的值。为此,我做了以下操作:
x = client_dict[input('Enter the client name:\n')]
print(x)
y = input('if ok enter y otherwise enter n:\n')
if y =='n':
lst = []
for i in range(len(x)):
x[i] = input('enter the correct header:\n')
lst.append(x[i])
client_dict[x] = lst
else:
pass
假设我在第一个输入中输入 client 1
然后输入 n
意味着我想更改值。然后,算法两次要求我输入所需的 header(因为客户端 1 有两个值),第一个 header 我写 hello
,第二个我写 world
.阵容如下:
Enter the client name:
client 1
['ABC', 'EFG']
if ok enter y otherwise enter n:
n
enter the correct header:
hello
enter the correct header:
world
我现在可以查看我的 client_dict
修改为:
{'client 1': ['hello', 'world'],
'client 2': ['MNO', 'XYZ'],
'client 3': ['ZZZ']}
这意味着代码做我想要的,但是当条件语句中的过程结束时,我也得到以下错误:
TypeError: unhashable type: 'list'
来自于此:client_dict[x] = lst
。所以我想知道我做错了什么?尽管代码有效,但似乎在重写字典时出现了一些问题?
我认为您不小心将值赋给了 client_dict 中的键。比如,您试图说一个值,即列表,是它出错所在行的新条目中的键。你可能想做这样的事情:
client_dict["client1"] = x
但用代表该客户名称的变量替换“client1”。你可能错误地认为 x 是你的客户的名字(也就是 key),但它实际上等于这个字典条目的 value ,因为这一行:
x = client_dict[input('Enter the client name:\n')]
这就是说“当我进入字典“client_dict”并访问键(input() 调用的结果)所在的位置时,将 x 分配给结果值”
写出你的字典中 kvp 的含义可能会有所帮助:
键应该是:字符串
值应为:列表
然后,检查您的代码并考虑所有内容的类型。这是 Python 的一个问题,因为它很容易混淆每个变量应该表示的类型,因为它是动态类型的
我更改了你的代码,试试这个:
client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}
x = input('Enter the client name:\n')
print(client_dict[x])
y = input('if ok enter y otherwise enter n:\n')
if y == 'n':
for i in range(len(client_dict[x])):
client_dict[x][i] = input('enter the correct header:\n')
else:
pass
抱歉,请立即尝试。 已编辑