6.00 Introduction to Computer Science and Programming lec5: Objects in Python
来源:互联网 发布:深度卷积网络怎么用 编辑:程序博客网 时间:2024/06/05 05:57
Lec5 主要介绍了Python中的集合类:数组、列表和词典。
首先用下面的例子介绍数组:
Test = (1, 2, 3, 4, 5)print Test[0]print Test[1] x = 100 divisors = ()for i in range(1,x): if x%i == 0: divisors = divisors+(i,)print divisors
Python中的数组是immutable的,也就是说是不可改的。这一点和Java、C++都不同,例如在上面的代码中若执行Test[0] = 0,则Python会报错。
在上面的代码中,divisors + (i,)也许有些难懂。在这里,+是连接符,两端的操作数都是数组。(i, )是包含一个元素 i 的数组,而(i)并不是一个数组。
继续用代码来介绍链表:
Techs = ['MIT', 'Cal Tech']Ivys = ['Harward', 'Yale', 'Brown']Univs = []Univs.append(Techs)print 'Univs =', UnivsUnivs.append(Ivys)print 'Univs =', Univsfor e in Univs: print 'e =', eflat = Techs + Ivysprint 'flat =', flatartSchools = ['RISD', 'Harward']for u2 in artSchools: if u2 in flat: flat.remove(u2)print 'flat =', flatflat.sort()print 'flat =', flatflat[1] = 'UMass'print 'flat =', flat从上面可以看出,List是一个典型的类,有append, remove, sort等方法。而数组则更像Java中的int等primitive类型,只能被操作符操作。
下面通过代码介绍Dictionary:
EtoF = {'bread': 'du pain', 'wine': 'du vin',\ 'eats': 'mange', 'drinks': 'bois',\ 'likes': 'aime', 1: 'un',\ '6.00':'6.00'}print EtoFprint EtoF.keys()print EtoF.keysdel EtoF[1]print EtoFD = {1: 'one', 'deux': 'two', 'pi':3.14159}D1 = Dprint D1for k in D1.keys(): print k, '=', D1[k]def translateWord(word, dictionary): if word in dictionary: return dictionary[word] else: return worddef translate(sentence): translation = '' word = '' for c in sentence: if c != ' ': word = word + c else: translation = translation + ' '\ + translateWord(word, EtoF) word = '' return translation[1:] + ' ' + translateWord(word, EtoF)print translate('John eats bread')print translate('Steve drinks wine')print translate('John likes 6.00')def keySearch(L, k): for elem in L: if elem[0] == k: return elem[1] return None
Clone方法:
学习过Java的同学应该对这个方法很熟悉,mutable的类需要使用Clone方法才能在内存中获得一个新的对象。
- 6.00 Introduction to Computer Science and Programming lec5: Objects in Python
- 6.00 Introduction to Computer Science and Programming lec1
- 6.00 Introduction to Computer Science and Programming lec2
- 6.00 Introduction to Computer Science and Programming lec3 & lec4
- 6.00 Introduction to Computer Science and Programming lec6: Debugging
- 6.00 Introduction to Computer Science and Programming Lec 9: Set
- MIT《Introduction to Computer Science and Programming》: Testing and Debugging
- 【学习笔记】Introduction to Computer Science and Programming
- 6.00 Introduction to Computer Science and Programming Lec 8: Efficiency and Order of Growth
- 6.00 Introduction to Computer Science and Programming Lec 9: Lecture 9: Memory and Search Methods
- mit 6.00 introduction to computer science using Python video note
- 看后总结(Introduction to Computer Science and Programming,MIT 600)
- Introduction to Parallel and Concurrent Programming in Python
- Computer Science and Programming: Books and Resources
- 6.087 Practical Programming in C, lec5: Pointers and memory addressing
- An Introduction to Interactive Programming in Python
- introduction to computation and programming using python
- An Introduction to Interactive Programming in Python (Part 2) week 6 (Classes and Tied images)
- WinDbg分析DMP文件方法完全攻略
- 软件调试的一般思路
- asp.net开发中,web页面实现页面元素的多语言化(多国语化)
- 7K面试题,交通灯管理系统
- oracle expdb导出目录名无效
- 6.00 Introduction to Computer Science and Programming lec5: Objects in Python
- codeforces 258B Little Elephant and Elections 数位DP+DFS
- 随机数算法
- PHP扩展开发-07-函数调用
- INT 2DH中断
- hdu 2089 不要62 数位DP
- POJ 3252 Round Numbers 数位DP
- Windows下Red5安装与使用入门
- 天空之城