1. 程式人生 > >集合 相關 深淺copy

集合 相關 深淺copy

del name remove list section upd enum bsp 賦值

‘‘‘
集合:可變的數據類型,他裏面的元素必須是不可變的數據類型,無序,不重復。
     {}
‘‘‘
# set1 = set({1,2,3})
# set2 = {1,2,3,[2,3],{‘name‘:‘alex‘}}  # 錯的
# print(set1)
# print(set2)
# set1 = {‘alex‘,‘wusir‘,‘ritian‘,‘egon‘,‘barry‘,}
#
#add
# set1.add(‘女神‘)
# print(set1)
#update
# set1.update(‘abc‘)
# print(set1)
#
刪除 # set1.pop() # 隨機刪除 # print(set1.pop()) # 有返回值 # print(set1) # set1.remove(‘alex‘) # 按元素 # print(set1) #{} set() # set1.clear() # print(set1) # set() # del set1 # print(set1) # # for i in set1: # print(i) # set1 = {1,2,3,4,5} # set2 = {4,5,6,7,8} # set3 = set1 & set2
# print(set3) # {4, 5} # print(set1.intersection(set2)) # {4, 5} # set1 = {1,2,3,4,5} # set2 = {4,5,6,7,8} # print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7,8} # print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7} # set1 = {1,2,3,4,5} # set2 = {4,5,6,7,8} # print(set1 ^ set2) # {1, 2, 3, 6, 7, 8}
# print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8} # set1 = {1,2,3,4,5} # set2 = {4,5,6,7,8} # print(set1 - set2) # {1, 2, 3} # #set1獨有的 # print(set1.difference(set2)) # {1, 2, 3} # set1 = {1,2,3,} # set2 = {1,2,3,4,5,6} # # print(set1 < set2) # print(set1.issubset(set2)) # 這兩個相同,都是說明set1是set2子集。 # print(set2 > set1) # print(set2.issuperset(set1)) # 這兩個相同,都是說明set2是set1超集。 #去重 # li = [1,2,33,33,2,1,4,5,6,6] # set1 = set(li) # # print(set1) # li = list(set1) # print(li) # s1 = {1,2,3} # print(s1,type(s1)) # s = frozenset(‘barry‘) # print(s,type(s)) # for i in s: # print(i)

深淺copy

# 賦值運算
# l1 = [1,2,3]
# l2 = l1
# l1.append(‘a‘)
# print(l1,l2)

#copy
# l1 = [1,2,3]
# l2 = l1.copy()
# print(l1,l2)
# print(id(l1),id(l2))
# l2.append(‘a‘)
# print(l1,l2)

# l1 = [1,2,[4,5,6],3]
# l2 = l1.copy()
#
# print(l1,id(l1))
# print(l2,id(l2))
# l1.append(‘a‘)
# print(l1,l2)
# l1[2].append(‘a‘)
# print(l1,l2)
# print(id(l1[2]))
# print(id(l2[2]))
import copy
# l1 = [1,2,[4,5,6],3]
# l2 = copy.deepcopy(l1)
# print(l1,id(l1))
# print(l2,id(l2))
# l1[2].append(‘a‘)
# print(l1,l2)

# l1 = [1,[1],2,3,4]
# l2 = l1
# # l1[1].append(‘a‘)
# # l2 的結果是什麽?
#
#
# print(l1,id(l1))
# print(l2,id(l2))
# print(l1[1] is l2[1])

# li = [‘alex‘,‘taibai‘,‘wusir‘,‘egon‘]
# for i in li:
#     print(li.index(i),i)

# for index,i in enumerate(li,1):
#     print(index,i)

集合 相關 深淺copy