集合set,函数
2018-01-08 21:21
211 查看
集合
集合的定义:集合是不重复的数据结构 *如果{}里面为空,为字典类型 In [1]: a = {} In [3]: type(a) Out[3]: dict *定义集合的第一种方式 In [14]: a = {2,3,4,2,3} In [15]: a Out[15]: {2, 3, 4} In [16]: type(a) Out[16]: set *定义集合的第二种方式,定义一个空的集合,工厂法定义集合时,括号里面是可迭代对象,eg:数值类型不可以 In [7]: a = set() In [8]: type(a) Out[8]: set In [12]: a = set([1,2,3,1,2,3]) In [13]: a Out[13]: {1, 2, 3} 应用:如何实现列表去重? *转化为集合数据类型;set(列表) In [19]: li = [1,2,2,3,3,4,4,5,5] In [39]: s = set(li) In [40]: li = set(s) In [41]: li Out[41]: {1, 2, 3, 4, 5} *去重2 In [36]: li1 = {}.fromkeys(li) In [38]: li1.keys() Out[38]: [1, 2, 3, 4, 5] 例: #!/usr/bin/env python #coding:utf-8 import random d = set() ##定义为集合 N = input(":") for i in range(N): d.add(random.randint(1,1000)) ##将随机数添加到集合里 #li = list(d) ##转化为列表 #li.sort() #print li print sorted(d) ##sorted是内置方法,用来排序
集合的增删改查: *增 s.add In [29]: s Out[29]: {2, 3, 4, 'hello', 'westos'} In [30]: s.add(5) In [31]: s Out[31]: {2, 3, 4, 5, 'hello', 'westos'} s.update In [33]: s1 = {'a','b'} In [34]: s.update(s1) In [35]: s Out[35]: {2, 3, 4, 5, 'a', 'b', 'hello', 'westos'} *删 s.pop() ##随意删除一个 s.remove() ##指定的元素,如果不存在,报错 s.discard() ###指定的元素,如果不存在,do nothing s.clear() ##清空集合元素 *改 s.update *查(关系测试操作) In [1]: d1 = {2,3,4,5,6} In [2]: d2 = {3,4,5,7} 交集 In [3]: d1 & d2 Out[3]: {3, 4, 5} In [7]: d1.intersection(d2) Out[7]: {3, 4, 5} 并集 In [4]: d1 | d2 Out[4]: {2, 3, 4, 5, 6, 7} In [8]: d1.union(d2) Out[8]: {2, 3, 4, 5, 6, 7} 差集 In [5]: d1 - d2 Out[5]: {2, 6} In [9]: d1.difference(d2) Out[9]: {2, 6} In [10]: d2.difference(d1) Out[10]: {7} 对等差分 In [6]: d1 ^ d2 Out[6]: {2, 6, 7} In [11]: d1.symmetric_difference(d2) Out[11]: {2, 6, 7}< 4000 /span> ............................................ In [12]: d1 = {1,2,3,4} In [13]: d2 = {1,2,3} In [14]: d1.issubset(d2) ##d1是否是d2的子集 Out[14]: False In [15]: d2.issubset(d1) Out[15]: True In [16]: d1.issuperset(d2) ##d1是否包含d2 Out[16]: True In [17]: d1.isdisjoint(d2) ##d1是否不是d2 Out[17]: False In [18]: d2.isdisjoint(d1) Out[18]: False
字典的特性: *集合时无序的,不重复的数据类型 *故不支持索引,切片,重复,连接 *支持成员操作符 *支持for循环,是可迭代的
总结: *不可变数据类型:数值类型,字符串,元组,实现某个功能直接改变可变的数据类型 *可变数据类型:列表,集合,字典,实现某个功能需要将结构赋值给另外一个变量 *可迭代数据类型(能否实现for循环):str,list,tuple,dict,set *不可迭代数据类型 *有序的数据类型(是否支持索引,切片,重复连接):str,list,tuple *无序的数据类型:dict,set
函数
*函数的定义 def 函数名(): *函数调用 函数名() 定义函数时,函数不执行;调用函数时,函数才执行 *有参数的函数 必选参数 #形式参数 def aaa(x,y): print x + y #实参,x=2,y=4 aaa(2,4) 默认参数 def aaa(x,y=2): print x**y aaa(3) 可变参数 def aaa(*args): #args实际上是一个元组 #print args sum = 0 for i in args: sum += i print sum aaa(1,2,3,45,6) 返回值(函数中如果没有return时,默认返回None) def add(x,y): print x + y #return None print add(1,2) 3 None 返回多个值 def fun(*args): """ :param args: :return: """ return max(args),min(args) print fun(22,2,3,12,34) 在pycharm里全部选中然后再ctrl+/,集体出现#
*函数的作用域 #全局变量(gobal关键字必须要声明,再赋值) num = 1 def fun(): #global声明num为全局变量 global num num = 5 fun() print num
*高级特性 索引 切片 from collections import Iterable print isinstance("hello",Iterable) print isinstance([1,23,4],Iterable) print isinstance({'a':1},Iterable) print isinstance({1,2,3},Iterable) True True True True 列表生成式 -生成列表的公式 -需求:生成一个列表,返回1-100中偶数的平方 方法1 li = [] for i in range(2,20,2): li.append(i**2) print li 方法2 print [i**2 for i in range(2,20,2)] 变异的列表生成式 #for循环嵌套if语句 print [i**2 for i in range(2,20) if i%2==0] #for循环嵌套for循环,两个字符串的全排列 print [i+j for i in 'xyz' for j in '123' ] 练习:找出/etc下文件中以.com结尾的文件 提示: os.listdir("/etc") ##找出/etc下的文件 s.endswith(".conf") ##找出以.conf结尾的文件 import os print [a for a in os.listdir('/etc') if a.endswith('.conf')][:5] ##[:5]表示显示前五个
相关文章推荐
- Python基础之set集合与函数
- Scala集合,序列(可变和不可变List),List各种函数的使用,不可变Set和可变Set,Map
- set集合容器自定义比较函数
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- Python Set集合,函数,深入拷贝,浅入拷贝,文件处理--小白基础
- 子模集合函数(Submodular set function)
- C++STL_set中函数_集合运算
- Python学习笔记:集合(set)有关函数
- 子模集合函数(Submodular set function)
- set 集合 和 函数
- python开发学习-day03(set集合、collection系列 、深浅拷贝、函数)
- 初学Python的学习笔记2----dist字典,set集合,声明函数,函数参数
- python--set集合下的函数
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- java基础之集合函数-Set
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- 00106 python函数:set()集合
- test_and_set_bit()等小函数集合
- java 集合之Set
- 集合框架(List 、Set)