python 3--リスト操作-深いコピー、浅いコピー、遍歴

3097 ワード

リスト操作-浅いコピー(copy)、深いコピー
1.浅いコピー(copy)、深いコピー
1.1浅いコピー
# coding:utf-8
# python3 -- list    (  copy)

#         :       copy     

#    ,      ,2             
list_names = ["he", "li", ["liu", "li"], "fu", "chen"]
list_names2 = list_names.copy()
list_names[3] = " "
print(list_names)
print(list_names2)

#   name,   list_names        
name = list_names
print(name)
#     :,  2      ,          
list_names[2][0] = " "
print(list_names)
print(list_names2)

2.深いコピー
# coding:utf-8
# python3 -- list    (   copy)

import copy

#    :            list_names        
list_names = ["he", "li", ["liu", "li"], "fu", "chen"]
list_names2 = copy.deepcopy(list_names)
list_names[3] = " "
print(list_names)
print(list_names2)

#     
list_names[2][0] = " "
print(list_names)
print(list_names2)

3.パスリストスライス
# coding:utf-8
# python3 -- list     (  )
list_names = ["he", ["liu", "li"], "li",  "fu", "chen", "liu", "gao"]
#     
# range(1, 10, 2)
print(list_names[0:-1:2])
print(list_names[::2])
#   0   -1 ,      
print(list_names[:])

for i in list_names:
    print(i)