
上QQ阅读APP看书,第一时间看更新
8-8 列表与元组数据互换
程序设计过程,也许会有需要将列表(list)与元组(tuple)数据类型互换,可以使用下列指令。
list( ):将元组数据类型改为列表。 tuple( ):将列表数据类型改为元组。
程序实例ch8_10.py:重新设计ch8_8.py,将元组改为列表的测试。

执行结果

上述第4行由于list_keys已经是列表,所以可以使用append( )方法。
程序实例ch8_11.py:将列表改为元组的测试。

执行结果

上述前5行程序是正确的,所以可以看到有分别打印列表和元组元素,程序第6行的错误是因为tuple_keys是元组,不支持使用append( )增加元素。