iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 4
0

Mutable

list有mutable的特性,意即該物件在創建後可以被修改。要如何確認呢?使用id(),物件內容會變但id不會變。與之對應的概念是immutable,意即該物件在創建後不能被修改。

有點類似Java當中StringStringBuffer

# list is mutable ,string is immutable
demo = [x for x in range(10)]
print( "demo data:{}".format(demo) ) 
print( "id of demo data:{}".format(id(demo)) )

# demo data:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# id of demo data:140286988042504

demo.append(11)
print( "demo data after appending:{}".format(demo) )
print( "id of demo data after appending:{}".format(id(demo)) )

# demo data after appending:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
# id of demo data after appending:140286988042504

s1 = demo[:]
print( "sliced 1:{}".format(s1) )
print( "id of demo data after slicing:{}".format(id(demo)) )
print( "id of s1 data:{}".format(id(s1)) )

# sliced 1:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
# id of demo data after slicing:140286988042504
# id of s1 data:140286988043016

str1 = 'ironman'
str2 = 'ironman'
print( "id of str1:{0} ; id of str2:{1}".format(id(str1), id(str2)) )

# id of str1:140287009764160 ; id of str2:140287009764160

在創建字串時可以用+連結或是利用list mutable的特點減少資源,但感覺python有針對+連結字串優化,在第一次+後,第二次開始所對應到的物件id看起來是同一個。

# build str
source_str = ['i', 'r', 'o', 'n', 'm', 'a', 'n']

final_str = ""
print( "id of final_str:{}".format(id(final_str)) )
for x in source_str:
    final_str += x
    print( "final str after +:{}".format(final_str) )
    print( "id of final_str after +:{}".format(id(final_str)) )
print( "final str:{}".format(final_str) )

# id of final_str:140287011039920
# final str after +:i
# id of final_str after +:140287010169328
# final str after +:ir
# id of final_str after +:140287009764944
# final str after +:iro
# id of final_str after +:140287009764944
# final str after +:iron
# id of final_str after +:140287009764944
# final str after +:ironm
# id of final_str after +:140287009764944
# final str after +:ironma
# id of final_str after +:140287009764944
# final str after +:ironman
# id of final_str after +:140287009764944
# final str:ironman

str_list = []
print( "str_list:{}".format(str_list) )
for x in source_str:
    str_list.append(str(x))
    print( "str_list after appending:{}".format(str_list) )
    print( "id of str_list after appending:{}".format(id(str_list)) )
print( "str_list with join:{}".format("".join(str_list)) )

# str_list:[]
# str_list after appending:['i']
# id of str_list after appending:140286988042888
# str_list after appending:['i', 'r']
# id of str_list after appending:140286988042888
# str_list after appending:['i', 'r', 'o']
# id of str_list after appending:140286988042888
# str_list after appending:['i', 'r', 'o', 'n']
# id of str_list after appending:140286988042888
# str_list after appending:['i', 'r', 'o', 'n', 'm']
# id of str_list after appending:140286988042888
# str_list after appending:['i', 'r', 'o', 'n', 'm', 'a']
# id of str_list after appending:140286988042888
# str_list after appending:['i', 'r', 'o', 'n', 'm', 'a', 'n']
# id of str_list after appending:140286988042888
# str_list with join:ironman

Assignment

利用=指定list到另一個變數,原變數和新變數所對應到的list物件為同一個

# assign
demo2 = [x for x in range(20)]
print( "demo2 data:{}".format(demo2) )
print( "id of demo2 data:{}".format(id(demo2)) )
demo3 = demo2
print( "demo3 data:{}".format(demo3) )
print( "id of demo3 data:{}".format(id(demo3)) )

# demo2 data:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# id of demo2 data:140286988042824
# demo3 data:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# id of demo3 data:140286988042824

參考


上一篇
Day3-List-操作
下一篇
Day5-List-常用方式
系列文
if len(learning.python) == 30:31

尚未有邦友留言

立即登入留言