溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python集合set和創建對象的復制方法是什么

發布時間:2022-01-13 14:51:53 來源:億速云 閱讀:230 作者:iii 欄目:大數據

本文小編為大家詳細介紹“python集合set和創建對象的復制方法是什么”,內容詳細,步驟清晰,細節處理妥當,希望這篇“python集合set和創建對象的復制方法是什么”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

集合set

輸入:

bri = set(['brazil', 'russia', 'india'])

print('india' in bri) 


print('usa' in bri )


bric = bri.copy() 

bric.add('china') 

print( bric.issuperset(bri) )


bri.remove('russia') 

print( bri & bric )

# OR bri.intersection(bric) 

輸出:

True

False

True

{'brazil', 'india'}

解釋:

集合集是簡單對象的無序集合。集合相比與順序性的數據結構,更注重與集合中的對象是否存在,以及發生多少次。

本例中,首先定義了set集合名為bri.

然后使用in語句來判斷'india','usa',兩個對象是否在集合中,輸出結果會根據對象是否在集合中,返回bool值 TRUE 或者 False 。

集合有copy()函數,可以復制出一份新的集合。

集合可以調用.remove() 來刪除其中的某個對象。

可以進集合運算,本例中進行取交集運算。

輸出結果為:{'brazil', 'india'}

創建對象的復制操作

輸入:

#!/usr/bin/python 

# Filename: reference.py 


print('Simple Assignment') 

shoplist = ['apple', 'mango', 'carrot', 'banana']mylist = shoplist 

# mylist is just another name pointing to the sameobject! 


del shoplist[0] 

# I purchased the first item, so I remove it from thelist 


print('shoplist is', shoplist) 

print('mylist is', mylist) 

# notice that both shoplist and mylist both print the same list without 

# the 'apple' confirming that they point to the same object 


print('Copy by making a full slice') 

mylist = shoplist[:] # make a copy by doing a full slice 


del mylist[0] 

# remove first item 

print('shoplist is', shoplist) 

print('mylist is', mylist) 

# notice that now the two lists are different

輸出:

$ python reference.py 

Simple Assignment 

 shoplist is ['mango', 'carrot', 'banana'] 

 mylist is ['mango', 'carrot', 'banana'] 

 Copy by making a full slice 

 shoplist is ['mango', 'carrot', 'banana'] 

 mylist is ['carrot', 'banana']

解釋:

當你創建對象并將其分配給變量時,該變量僅引用該對象,并不代表對象本身!可以理解為分配了一個地址鏈接。

本例中舉了兩組例子。

首先建立一個列表shoplist,我們在拷貝復制出一個列表mylist。 拷貝方式使用 “=” 賦值。

我們刪除 shoplist中的一個元素,發現mylist中的元素也被刪除了。

第二次,我們同樣拷貝復制出一個列表mylist。 拷貝方式使用 “mylist = shoplist[:]” 賦值。

我們刪除 shoplist中的一個元素,發現mylist中的元素還存在。

第一中,方式我們只復制出來對象的地址映射,而第二次,我們完整的賦值了對象的內容。

讀到這里,這篇“python集合set和創建對象的復制方法是什么”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女