话说元组就是被包含在小括号里面,不能被修改。列表是包含在中括号里面,可以被修改。 列表中可以嵌套列表,元组中可以嵌套元组,一般没人混用,这点测试无误: [python] view plain copy >>> aa[0] (12, 34) >>> aa[0]=(1,2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘tuple‘ object does not support item assignment >>> aa[0][0] 12 >>> aa[0][0]=34 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘tuple‘ object does not support item assignment 然而,在画图过程中使用了字典,无意中修改了“元组”的值: [python] view plain copy >>> pos = {0: (20, 20), 1: (20, 40), 2: (40, 40), 3: (40, 20), 4: (30, 30)} >>> pos {0: (20, 20), 1: (20, 40), 2: (40, 40), 3: (40, 20), 4: (30, 30)} >>> pos[0]=(1,2) >>> pos {0: (1, 2), 1: (20, 40), 2: (40, 40), 3: (40, 20), 4: (30, 30)} >>> 然后在参考手册的dict搜索发现了这个: [python] view plain copy >>> a = dict(one=1, two=2, three=3) >>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3} >>> c = dict(zip([‘one‘, ‘two‘, ‘three‘], [1, 2, 3])) >>> d = dict([(‘two‘, 2), (‘one‘, 1), (‘three‘, 3)]) >>> e = dict({‘three‘: 3, ‘one‘: 1, ‘two‘: 2}) >>> a == b == c == d == e True 也就是说,在字典中value值无论用小括号、中括号、大括号括起来,它的值都可以被修改。 另一个误导是参考python中元组和小括号的关系,即元组是由逗号决定的,不是小括号。可以看到,即便没有了小括号,还是元组。 [python] view plain copy >>>a=("one","two") >>>a[0] ‘one‘ >>>b=("one") >>>b[0] ‘o‘ >>>c=("one",) >>>c[0] ‘one‘ >>>d="one", >>>d[0] one 顶