Discuss / Python / 打卡第七天,连续学习七天python

打卡第七天,连续学习七天python

Topic source

因为是 (1, [2, 3]),因为元组里面有列表,所以,插入 dict 和 set 的时候会报错:

a = (1, 2, 3)
dict1 = {'ast': 1, 'abc': 2}
dict1[a] = 1print(dict1)
set1 = {1, 2, 3}
set1.add(a)
print(set1)

b = (1, [2, 3])
dict1[b] = 2print(dict1)
set1.add(b)
print(set1)

# 输出结果: 
{'ast': 1, 'abc': 2, (1, 2, 3): 1}
{1, 2, 3, (1, 2, 3)}
TypeError: unhashable type: 'list'

寒山

#2 Created at ... [Delete] [Delete and Lock User]

请问第三行代码什么意思呢

Promethues

#3 Created at ... [Delete] [Delete and Lock User]
dict1[a] = 1print(dict1)

代表增加一个 key为a,value 为print(dict1)输出的一个 字典。print前的1应该是手误输入的。

diasarah

#4 Created at ... [Delete] [Delete and Lock User]
dict1[a] = 1print(dict1)

这句应该是中间没有换行,在1后面应该有个回车,就酱


  • 1

Reply