PYTHON機器學習自學/自修 整理[00008] ~ 語言技術:PYTHON GOSSIP(Tuple 處理)
PYTHON機器學習自學/自修 整理[00008] ~ 語言技術:PYTHON GOSSIP(Tuple 處理)
Tuple就像是串列(List),不過串列是可變動(Mutable)物件,而Tuple是不可變動(Immutable)物件。你可以使用()來建立Tuple物件
import sys data = (1, 'Justin', 93) print(data[0])#1 print(data[0:2])#(1, 'Justin') print(data[0:])#(1, 'Justin', 93) for elem in data: print(elem) t1 = (3, 6, 1, 2, 7) l1 = list(t1) print(l1)#[3, 6, 1, 2, 7] l1.sort() print(l1)#[1, 2, 3, 6, 7] t1 = tuple(l1) print(t1)#(1, 2, 3, 6, 7)