python学习笔记03

一.集合

把不同的元素组成一起形成集合,是python基本的数据类型。

集合元素(set elements):组成集合的成员

python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.  

sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。 

创建集合的操作:

a=set([10,2,2,4])
print(type(a))

1.集合操作

a = set([10,2,2,4])
b = set([2,3,4,5])
c1 = a.intersection(b) #返回两个集合的交集
print(c1)
c2 = a.union(b) #返回两个集合的并集
print(c2)
c3 = a.difference(b) #求两个集合的差集
print(c3)
c4 = a.issubset(b) #判断是否为子集
print(c4)
c5 = a.issuperset(b) #判断是否为父集
print(c5)
c6 = a.symmetric_difference(b) #对称差集
print(c6)
c7 = a.isdisjoint(b) #判断是否有交集
print(c7)

二.文件操作

详情见

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820066616a77f826d876b46b9ac34cb5f34374f7a000

三.函数

python中函数与C++函数使用并无太大区别。使用def定义函数。需要注意的是python函数可以有多个返回值。

posted @ 2017-05-19 16:38  打包餐巾纸  阅读(180)  评论(0)    收藏  举报