国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Lecture 6 - Objects

2019-11-06 06:34:09
字體:
供稿:網(wǎng)友

Lecture 6 - Objects

1. Tuple

Compound data types:

TuplesLists Dictionaries

Tuple:

t1 = (1, ‘two’, 3) PRint(t1)

example:

## divisorsdef findDivisors(n1, n2): """assumes that n1 and n2 are positive ints returns a tuple containing the common divisors of n1 and n2""" divisors = () # the empty tuple for i in range(1, min(n1, n2) + 1): if n1%i == 0 and n2%i == 0: divisors = divisors + (i,) return divisorsdivisors = findDivisors(20, 100)total = 0for d in divisors: total += dprint(total)`

2. Lists

BIG DIFFERENCE WITH TUPLE:

Lists are mutable!! While tuple, int, float, str are immutable. So lists can be modified aMer they are created!

example:

## universitiesTechs = ['MIT', 'Cal Tech']Ivys = ['Harvard', 'Yale', 'Brown']Univs = [Techs, Ivys]Univs1 = [['MIT', 'Cal Tech'], ['Harvard', 'Yale', 'Brown']]Techs.append('RPI')print('Univs = ')print(Univs)print('')print('Univs1 =')print(Univs1)for e in Univs: print('Univs contains ') print(e) print(' which contains') for u in e: print(' ' + u)

3. OperaTIONS ON LISTS

Append:

Techs.append(Ivys)Then Techs returns [‘MIT’, ‘Cal Tech’, ‘RPI’, [‘Harvard’, ‘Yale’, ‘Brown’]]side effect: Creates a new listso we can use: flat = Techs + IvysThen flat returns [‘MIT’, ‘Cal Tech’, ‘RPI’,’Harvard’, ‘Yale’, ‘Brown’]

Cloning:

Avoid mutatating a list over which one is iterating

example:

def removeDups(L1, L2): for e1 in L1: if e1 in L2: L1.remove(e1)L1 = [1,2,3,4]L2 = [1,2,5,6]removeDups(L1, L2)

Then print(L1) returns [2, 3, 4]

WHY???

Inside for loop, Python keeps track of where it is in list using internal counter

When we mutate a list, we change its length but Python doesn’t update counter

Better is to clone

def removeDupsBetter(L1, L2): L1Start = L1[:] for e1 in L1Start: if e1 in L2: L1.remove(e1) L1 = [1,2,3,4] L2 = [1,2,5,6] removeDupsBetter(L1, L2)

- Note that using L1Start = L1 is not sufficient

4. FUNCTIONS AS OBJECTS

concept: Functions are first class objects

They have typesThey can be elements of data structures like listsThey can appear in expressions As part of an assignment statementAs an argument to a func%on!!

Example:

# applyToEachdef applyToEach(L, f): """assumes L is a list, f a function mutates L by replacing each element, e, of L by f(e)""" for i in range(len(L)): L[i] = f(L[i])L = [1, -2, 3.4]def fact(n): if n == 1: return 1 else: return n*fact(n-1)def fib(n): if n == 0 or n == 1: return 1 else: return fib(n-1) + fib(n-2)applyToEach(L, abs)print(L)applyToEach(L, int)print(L)applyToEach(L, fact)print(L)applyToEach(L, fib)print(L)

Map

對可迭代函數(shù)’iterable’中的每一個元素應(yīng)用‘function’方法,將結(jié)果作為list返回。

舉例: >>> def add100(x):... return x+100... >>> hh = [11,22,33]>>> map(add100,hh)[111, 122, 133]

如果給出了額外的可迭代參數(shù),則對每個可迭代參數(shù)中的元素‘并行’的應(yīng)用‘function’。(翻譯的不好,這里的關(guān)鍵是‘并行’)

>>> def abc(a, b, c):... return a*10000 + b*100 + c... >>> list1 = [11,22,33]>>> list2 = [44,55,66]>>> list3 = [77,88,99]>>> map(abc,list1,list2,list3)[114477, 225588, 336699]

看到并行的效果了吧!在每個list中,取出了下標(biāo)相同的元素,執(zhí)行了abc()。

如果’function’給出的是‘None’,自動假定一個‘identity’函數(shù)(這個‘identity’不知道怎么解釋,看例子吧)

>>> list1 = [11,22,33]>>> map(None,list1)[11, 22, 33]>>> list1 = [11,22,33]>>> list2 = [44,55,66]>>> list3 = [77,88,99]>>> map(None,list1,list2,list3)[(11, 44, 77), (22, 55, 88), (33, 66, 99)]

5. DICTIONARIES

Concept: Dict is generalization of lists, but now indices don’t have to be integers – can be values of any immutable type

Syntax:

monthNumbers = { ‘Jan’:1, ‘Feb’:2, ‘Mar’:3, 1:’Jan’, 2:’Feb’, 3:’Mar’}

Keys can be complex

myDict = {(1,2): 'twelve', (1,3): 'thirteen'}myDict[(1,2)]returns ‘twelve’
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 彭山县| 台州市| 鄱阳县| 慈利县| 新宾| 隆德县| 固始县| 临猗县| 云林县| 来凤县| 大庆市| 平武县| 海淀区| 那坡县| 定兴县| 康平县| 东丰县| 太和县| 兴化市| 正定县| 河曲县| 九寨沟县| 双牌县| 巴东县| 邮箱| 舟山市| 万年县| 扎囊县| 博兴县| 来宾市| 建德市| 息烽县| 梁河县| 台湾省| 通州市| 冀州市| 潮安县| 金乡县| 治多县| 平和县| 潼关县|