當前位置:編程學習大全網 - 編程語言 - python 按房屋類型和每壹房屋類型下臥室多少計算其平均售價

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 copy(self, *args, **kwargs): # real signature unknown

""" Return a shallow copy 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 <module>

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)

動態參數

  • 上一篇:計算機專業留學美國哪個大學研究生好?
  • 下一篇:國產和進口焊接機器人有什麽區別?
  • copyright 2024編程學習大全網