python tuple

来源:互联网 发布:淘宝如何认证大v 编辑:程序博客网 时间:2024/06/06 10:45

python的list格式是 t=[1,2],可以随时添加或删除里面元素
python的tuple格式是t=(1,2),一旦定义就不可以修改

以下转载自Eric2016_Lv

# -*- coding: utf-8 -*-"""Python Version: 3.5Created on Mon May  8 17:04:45 2017E-mail: Eric2014_lv@sjtu.edu.cn@author: DidiLv"""d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keysprint(d)t = (5, 6)       # Create a tupleprint(type(t))    # Prints "<type 'tuple'>"print(d[t])       # Prints "5"print(d[(1, 2)])  # Prints "1"# 这只是一个结合的dictionary的表示,没啥难的# 元组不是一个可以迭代的,更改的容器# 输出为:#{(0, 1): 0, (1, 2): 1, (5, 6): 5, (2, 3): 2, (4, 5): 4, (6, 7): 6, (8, 9): 8, (9, 10): 9, (3, 4): 3, (7, 8): 7}#<class 'tuple'>#5#1