導航:首頁 > 編程語言 > pythonisdisjoint

pythonisdisjoint

發布時間:2022-07-26 09:26:36

python 按房屋類型和每一房屋類型下卧室多少計算其平均售價

set數據類型
先用一行代碼來說明一下
#!/usr/bin/env python
s2={}
s = {33,12,33,32121}
for i in s:
print(i)
print(type(s))
print(type(s2))
s1=set()
s1.add(11)
s1.add(22)
s1.add(33)
print(s1)
下面的代碼的運行結果
32121
12
33
<class 'set'>
<class 'dict'>
{33, 11, 22}
通過代碼的結果可以看出
set是一個是一個無序且不重復的元素集合
創建set 集合和字典相同{} 只有通過內部的元素才能體現出區別
創建空set集合,最好使用set()的方法創建,然後通過add方法添加元素
以下是set集合的一些常用方法
class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
"""
def add(self, *args, **kwargs): # real signature unknown
"""
Add an element to a set,添加元素
This has no effect if the element is already present.
"""
pass
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. 清除內容"""
pass
def (self, *args, **kwargs): # real signature unknown
""" Return a shallow of a set. 淺拷貝 """
pass
def difference(self, *args, **kwargs): # real signature unknown
"""
Return the difference of two or more sets as a new set. A中存在,B中不存在
(i.e. all elements that are in this set but not the others.)
"""
pass
def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. 從當前集合中刪除和B中相同的元素"""
pass
def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing. 移除指定元素,不存在不保錯
"""
pass
def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set. 交集
(i.e. all elements that are in both sets.)
"""
pass
def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. 取交集並更更新到A中 """
pass
def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. 如果沒有交集,返回True,否則返回False"""
pass
def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. 是否是子序列"""
pass
def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. 是否是父序列"""
pass
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty. 移除元素
"""
pass
def remove(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError. 移除指定元素,不存在保錯
"""
pass
def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
Return the symmetric difference of two sets as a new set. 對稱差集
(i.e. all elements that are in exactly one of the sets.)
"""
pass
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. 對稱差集,並更新到a中 """
pass
def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set. 並集
(i.e. all elements that are in either set.)
"""
pass
def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. 更新 """
pass
三元運算
三元運算(三目運算),是對簡單的條件語句的縮寫。
# 書寫格式
result = 值1 if 條件 else 值2
# 如果條件成立,那麼將 「值1」 賦值給result變數,否則,將「值2」賦值給result變數
對於條件判斷的補充
當if的判斷語句中有 條件1 or 條件2 and 條件3的時候
按順序執行當條件一滿足的時候後面就不找了
因此需要改成( 條件1 or 條件2)and 條件3 才可以
深淺拷貝
首先說明
在下述環境中
當找到內存地址,就認為找到了數據內容
對於 數字 和 字元串
變數========房子名
內存地址(實際數據)=====房子地址
賦值======== 房子名—房子地址
內存========中介(有許多房源)
淺拷貝
看房子地址單
淺拷貝和深拷貝無意義,因為其永遠指向同一個內存地址。(房子地址)
對於字典、元祖、列表
字典、元祖、列表相當於房子在房子內部有許多房間
房子=[ 房間號,房間號,房間號]
因此字典、元祖、列表存儲的房子的房間號的集合===房屋房間號對照表
對於淺拷貝
相當於復制一份房屋房間號對照表
對於深拷貝
按照房屋房間號對照表蓋房
注意!!由於python內部對字元串和數字的優化 所以在最後一層( 按照房屋房間號對照表蓋房 )也指向同樣的地址
即可理解為
深拷貝,在內存中將所有的數據重新創建一份(排除最後一層,即:python內部對字元串和數字的優化)
函數
函數或者叫方法的目的是將程序中大量的重復代碼整合,使程序更加簡潔易懂。
函數式編程最重要的是增強代碼的重用性和可讀性
函數的定義
def 函數名(參數):
...
函數體
...
返回值
函數的定義通過def 關鍵字 +函數名定義
函數的定義主要有如下要點:
def:表示函數的關鍵字
函數名:函數的名稱,日後根據函數名調用函數
函數體:函數中進行一系列的邏輯計算,如:發送郵件、計算出 [11,22,38,888,2]中的最大數等...
參數:為函數體提供數據
返回值:當函數執行完畢後,可以給調用者返回數據。
系統內置函數
函數的返回值
函數是一個功能塊,該功能到底執行成功與否,需要通過返回值來告知調用者。
例如:
#!/usr/bin/env python
def funcl():
return "程序執行了"
r = funcl()
print(r)
return的返回值可以是字元串也可以是其它數據類型
默認情況下 return返回 None:
注意: 一旦遇到return以下的代碼將不再執行
函數的參數
定義函數的時候,我們把參數的名字和位置確定下來,函數的介面定義就完成了。對於函數的調用者來說,只需要知道如何傳遞正確的參數,以及函數將返回什麼樣的值就夠了,函數內部的復雜邏輯被封裝起來,調用者無需了解。
函數的參數就是給函數內部一個數據,使內部代碼既能重復執行又能產生不同結果 實現復用
例如 計算x的平方數
def power(x):
return x * x
>>> power(5)
25
>>> power(15)
225
通過改變x的值就可以得到任意數的平方數
函數的參數有3種類型
普通參數 例如剛才的例子中的x
默認參數
動態參數
默認參數
默認參數就給參數一個默認值。
例如
#!/usr/bin/env python
def power(x):
return x * x
power()
當給x值的時候程序便會報錯
Traceback (most recent call last):
File "C:/Users/zhang/PycharmProjects/untitled/blog.py", line 4, in <mole>
power()
TypeError: power() missing 1 required positional argument: 'x'
修改一下程序
#!/usr/bin/env python
def power(x=0):
return x * x
r =print(power())
這樣即便沒有給x值,但程序有一個默認值。因此程序正常運行
動態參數
函數的參數不單單可以是一個變數,也可以是列表,字典等
def func(*args):
print args
# 執行方式一
func(11,33,4,4454,5)
# 執行方式二
li = [11,2,2,3,3,4,54]
func(*li)
動態參數
def func(**kwargs):
print args
# 執行方式一
func(name='wupeiqi',age=18)
# 執行方式二
li = {'name':'wupeiqi', age:18, 'gender':'male'}
func(**li)
動態參數

② Python的集合有哪些操作

集合是一個無序的,不重復的數據組合,它有著兩個主要作用:去重以及關系測試。去重指的是當把一個列表變成了集合,其中重復的內容就自動的被去掉了

關系測試指的是,測試兩組數據之間的交集、差集、並集等關系。

去重測試代碼如下:

#創建一個列表--裡面存在一些重復值

test_list = [1,2,3,4,2,2,3,4,3,2,3,4]

#利用集合將列表中重復的內容去掉

test_list = set(test_list)

#列印測試並且查看test_list被賦予新值後的數據類型print(test_list,type(test_list)) # {1, 2, 3, 4}

Tip:需要注意的是,集合和字典一樣都是無序的。

獲取交集需要使用集合中的方法intersection方法,獲取兩個集合中的交集代碼如下:

#創建兩個集合,並且獲取集合的交集

test_list_01 = set(['YanYan','LiBai','LuLu','YangMi'])

test_list_02 = set(['YanYan','LiuDeHua','ZhangXueYou','LiBai'])

#在上面的兩個集合中,存在相同的值,那麼現在我們取出兩個集合中的交集

test_intersection = test_list_01.intersection(test_list_02)print(test_intersection) # {'YanYan', 'LiBai'}

獲取並集的方式需要採用集合中union方法,獲取兩個集合的並集代碼如下:

#創建兩個集合,並且獲取集合的交集

test_list_01 =set(['YanYan','LiBai','LuLu','YangMi'])

test_list_02 =set(['YanYan','LiuDeHua','ZhangXueYou','LiBai'])

#採用集合中的union方法獲取並集

test_list_union = test_list_01.union(test_list_02)

print(test_list_union) # {'LiBai', 'LuLu', 'ZhangXueYou', 'LiuDeHua', 'YangMi', 'YanYan'}

獲取差集的方式要採用集合中的difference方法,獲取兩個集合的差集的代碼如下所示:

#創建兩個集合,並且獲取集合的交集

test_list_01 = set(['YanYan','LiBai','LuLu','YangMi'])

test_list_02 = set(['YanYan','LiuDeHua','ZhangXueYou','LiBai'])

#使用集合中的difference方法來獲取差集

test_difference = test_list_01.difference(test_list_02)

test_difference2 = test_list_02.difference(test_list_01)

print(test_difference) # {'LuLu', 'YangMi'}print(test_difference2) # {'ZhangXueYou', 'LiuDeHua'}

判斷一個集合是否是另外一個集合的子集可以使用issubset()方法,同樣,還可以使用issuperset()方法判斷一個集合是否是另外一個集合的父級

代碼如下:

#創建兩個集合

list_set = set([1,2,3,4])

list_son = set([2,3])

#判斷list_son是否是list_set的子集print(list_son.issubset(list_set)) # True#判斷list_set是否是list_son的父級print(list_set.issuperset(list_son)) # True

對稱差集(又有人稱之為叫做反向差集),指的是取出兩個集合中互相都沒有的值取出放在一個集合中。

代碼如下:

#創建兩個集合

list_set_num1 = set([1,3,5,7,9])

list_set_num2 = set([2,3,4,6,9,10])

#獲取兩個集合的對稱差集print(list_set_num1.symmetric_difference(list_set_num2)) # {1, 2, 4, 5, 6, 7, 10}

如果上述的難以理解的話,可以對對稱差集理解為去掉兩個集合中都存在的內容,將剩餘的內容取到一個新的集合中。

除了上述的這些方法實現的關系功能之外,還有一個方法isdisjoint(),功能是判斷兩個集合中是否有相同的值,如果兩個集合中沒有相同的值(即沒有交集),那麼返回True

代碼如下:

#創建集合

test_set_num1 = set([1,2,3,4])

test_set_num2 = set([5,6,7,8])

test_set_num3 = set([1,3,7,8])

#使用isdisjoint()方法來判斷print(test_set_num1.isdisjoint(test_set_num2)) # Trueprint(test_set_num1.isdisjoint(test_set_num3)) # False

通過運算符來進行關系測試

在上面的應用中,主要是通過python中的方法進行的關系測試,那麼在python中,除了使用方法以外,還可以使用關系運算符來進行關系測試。

實例代碼如下:

test_list_01 =set(['YanYan','LiBai','LuLu','YangMi'])

test_list_02 =set(['YanYan','LiuDeHua','ZhangXueYou','LiBai'])

#獲取交集&print(test_list_01 & test_list_02) # {'LiBai', 'YanYan'}

#獲取並集|print(test_list_01 | test_list_02) # {'LuLu', 'LiBai', 'LiuDeHua', 'YanYan', 'ZhangXueYou', 'YangMi'}

#獲取差集-print(test_list_01 - test_list_02) # {'LuLu', 'YangMi'}print(test_list_02 - test_list_01) # {'LiuDeHua', 'ZhangXueYou'}

#獲取對稱差集print(test_list_01 ^ test_list_02) # {'ZhangXueYou', 'YangMi', 'LuLu', 'LiuDeHua'}

集合的增刪改查

添加

語法:Set.add()

代碼如下:

#創建一個集合

test_set =set(['YanYan'])

#添加

test_set.add('LiBai') #添加一項

test_set.update(['LuLu','JingJing']) #一次性向集合中添加多項

#輸出集合

print(test_set) #{'YanYan', 'LuLu', 'LiBai', 'JingJing'}

刪除

刪除集合中的某一個元素可以使用remove方法

代碼如下:

#創建一個集合

test_set = set(['YanYan'])

#使用remove方法刪除元素

test_set.remove('YanYan')print(test_set) # set()

刪除項目除了使用remove以外,還可以使用pop()方法,但是pop()方法刪除內容不能夠指定,只是隨機刪除。

pop方法會把刪除的內容返回,示例代碼如下:

#創建一個集合

test_set = set([20,9,'a',1,2,3,4])print(test_set.pop()) # 1print(test_set.pop()) # 2print(test_set.pop()) # 3

刪除元素還可以使用discard()方法,這個方法沒有返回值,如果列印返回值的話會輸出None

#創建一個集合

test_list = set([1,2,3,4,5])

#使用discard()方法刪除--注意,discard()方法刪除返回None,也就是沒有返回值print(test_list.discard(3)) # None#此時原集合中的3已經被刪除了print(test_list) # {1, 2, 4, 5}

查詢

xins #判斷x是否是s的成員

xnotins 判斷x是否是s的成員

len(x) #查看x的長度

s <= t #測試是否s中的每一個元素都在t中

s >= t #測試是否t中的每一個元素都在s中


閱讀全文

與pythonisdisjoint相關的資料

熱點內容
為什麼空氣難壓縮是因為斥力嗎 瀏覽:641
郭天祥單片機實驗板 瀏覽:599
伺服器有什麼危害 瀏覽:256
飢荒怎麼開新的獨立伺服器 瀏覽:753
文件夾變成了 瀏覽:560
linuxpython綠色版 瀏覽:431
怎麼下載小愛同學音箱app 瀏覽:554
python佔位符作用 瀏覽:76
javajdbcpdf 瀏覽:543
php網頁模板下載 瀏覽:192
python試講課pygame 瀏覽:409
安居客的文件夾名稱 瀏覽:677
家裡伺服器如何玩 瀏覽:451
網站源碼使用視頻 瀏覽:748
stc89c52單片機最小系統 瀏覽:452
郵件安全證書加密 瀏覽:416
雲伺服器如何訪問百度 瀏覽:279
常州電信伺服器dns地址 瀏覽:839
用小方塊製作解壓方塊 瀏覽:42
圖像壓縮編碼實現 瀏覽:68