Discuss / Python / 交作业

交作业

Topic source

把(1, 2, 3)和(1, [2, 3])放入dict或set中,并解释结果。

a = (1,2,3) b = (1,[2,3])

d1 = {a} print(d1) # {(1, 2, 3)}

d2 = {b}

print(d2) # Traceback (most recent call last): File "C:\Users\ken\AppData\Local\Temp\learn_python_q_m8k2bc_py\test_41.py", line 8, in <module> d2 = {b} TypeError: unhashable type: 'list'

s1 = set([a])

print(s1) # {(1, 2, 3)}

s2 = set([b])

print(s2) # Traceback (most recent call last): File "C:\Users\ken\AppData\Local\Temp\learn_python_q_m8k2bc_py\test_43.py", line 5, in <module> s2 = set([b]) TypeError: unhashable type: 'list'

a = (1,2,3) b = (1,[2,3])

d1 = {a:123} print(d1) # {(1, 2, 3): 123}

d2 = {a} print(d2) # {(1, 2, 3)}

s1 = set([a])

print(s1) # {(1, 2, 3)}

a = (1,2,3) b = (1,[2,3])

d1 = {a:123} print(d1) # {(1, 2, 3)}

print(d1[a]) # 123

上面那个粘错了

a = (1,2,3) b = (1,[2,3])

d1 = {a:123} print(d1) # {(1, 2, 3): 123}

print(d1[a]) # 123


  • 1

Reply