Python第四課 map、函數引用、排序、對象

來源:互聯網
上載者:User

標籤:lambda   根據   結果   算數   更新   dem   --   int   一個   

#可變類型的對象為全域變數時
#MAP 可以直接迭代,不需要寫for迴圈了
#方法一:
foo = [2,18,9,22,17]
# mapdemo = map(lambda x:x*2+10,foo)
# for i in mapdemo:
# print(i)

# #方法二:
# def tt(x):
# return x*2+10
# m=tt
# print("m={0}".format(m))
# mresult=m(12)
# print("mresult=%d"%mresult)
# result=map(tt,foo)
# print("result:{0}".format(result))


#加括弧是函數的引用,不加括弧是調用函數

# f=filter(lambda x:x>10,foo)#過濾滿足條件的結果
# print(f)
# for i in f:
# print(i)
#
# #reduce()求加減函數
# from functools import reduce
# # r=reduce(lambda x,y:x+y,foo)
# # print("r={0}".format(r))
# def re(x,y):
# return x+y
# r=reduce(re,foo)
# print("r={0}".format(r))
#
# #算數函數直接調用
# print(sum(foo))
# print(max(foo))
# print(min(foo))
#
# print(sorted(foo))#sorted函數會產生一個新的列表,不改變原來列表的值
# print(foo)
#
# s=sorted(foo,reverse=True) #倒序排序
# print(s)
#
# dict1={"a":1,"d":3,"c":2}
# print(sorted(dict1.items()))

# def _gt_(self,other):
# if self.obj>other:
# self.obj=obj
#
# print(sorted([1,2,3,4,5,6,7],key=reversed_cmp()))

#.sort()對原來的列表進行排序後會更新原來列表的值,沒有傳回值 sorted函數會產生一個新的列表,不改變原來列表的值 並且有傳回值

# a=[‘bbb‘,‘aaa‘,‘CCV‘,‘Zoo‘]
# print(sorted(a,key=str.lower))#根據字串大小寫進行排序
#
# #self代表當前對象
# class Person:
# def __init__(self,sex=None):#初始化函數
# self.sex=sex
# def eat(self):
# print("正在吃飯")
# def sleep(self):
# print("正在睡覺....")
# def say(self):
# print(self.name,self.age,self.sex)
# p=Person("女")
# p.eat()
# p.sleep()
#
# #添加屬性
# p.name=‘手機‘
# p.age=85
# p.say()

class Student(object):
def __init__(self,name=None,age=None,sex=None):
self.name=name
self.age=age
self.sex=sex
def study(self):
print("{0}在學習".format(self.name),"年齡:{0}".format(self.age),"性別:{0}".format(self.sex))
a=Student(‘王五‘,18,‘女‘)
a.study()

#封裝
#get/set 不寫_init_() 在init裡都是公用屬性
#私人方法def__study() 以--開頭,只能在函數內部使用
#自動銷毀方法__del__

def _del_(self):
print("對象正在被銷毀.....")
s=Student("張三")
s.study()
print(s)

Python第四課 map、函數引用、排序、對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.