码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记-(七)python基础--集合、文件操作&函数

时间:2016-08-07 18:20:15      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

本节内容

1.集合操作

2.文件操作

3.字符编码与转码

4.函数操作


1.集合操作

集合是一个无序的、不重复的数据组合;

1.1 常用操作

它的作用是:

1)自动去重:列表变成集合,自动去重;

>>> list_1 = [1,4,4,5,6,7,9,10]
>>> list_1 =set(list_1)
>>> print(list_1)
{1, 4, 5, 6, 7, 9, 10} 

2)关系测试:测试两组数据之间的关系,交集、并集、差集、对称差集、父集、子集等

#交集 intersection          &
>>> list_1 = set([1,4,4,5,6,7,9,10])
>>> list_2 = set([2,45,6,11])
>>> print(list_1.intersection(list_2)) #list_1和list_2的交集
{6}
>>> print(list_1 & list_2)
{6}
#并集 union    |
>>> list_1 = set([1,4,4,5,6,7,9,10])
>>> list_2 = set([2,45,6,11])
>>> print(list_1.union(list_2))
{1, 2, 4, 5, 6, 7, 9, 10, 11, 45}
>>> print(list_1 | list_2)
{1, 2, 4, 5, 6, 7, 9, 10, 11, 45}
#差集 in list_1 but not in list_2   ----difference
>>> list_1 = set([1,4,4,5,6,7,9,10])
>>> list_2 = set([2,45,6,11])
>>> print(list_1.difference(list_2))
{1, 4, 5, 7, 9, 10}
>>> print(list_1 - list_2)
{1, 4, 5, 7, 9, 10}
#对称差集 symmetric_difference ----去掉两者相同的数据后合并
>>> list_1 = set([1,4,4,5,6,7,9,10])
>>> list_2 = set([2,45,6,11])
>>> print(list_1.symmetric_difference(list_2))
{1, 2, 4, 5, 7, 9, 10, 11, 45}
>>> print(list_1 ^ list_2)
{1, 2, 4, 5, 7, 9, 10, 11, 45}
#子集 issubset
>>> list_1 = set([1,2,3,4,5,6])
>>> list_2 = set([1,4])
>>> print(list_1.issubset(list_2))
False
>>> print(list_2.issubset(list_1))
True
#父集 issuperset
>>> list_1 = set([1,2,3,4,5,6])
>>> list_2 = set([1,4])
>>> print(list_1.issuperset(list_2))
True
>>> print(list_2.issuperset(list_1))
False
#没有交集则为true,有交集为false
>>> list_1 = set([1,2,3,4,5,6])
>>> list_2 = set([1,4])
>>> print(list_1.isdisjoint(list_2))
false

1.2 基本操作

  • 添加
#添加一项
>>> list1 = set([1,3,4,6,7])
>>> list1.add(10)
>>> print(list1)
{1, 3, 4, 6, 7, 10}
#添加多项
>>> list1 = set([1,2])
>>> list1.update([6,8,10])
{8, 1, 2, 10, 6}
  • 删除
#remove ---删除不存在的元素会报错
>>> list1 = set([1,2,6,8,10])
>>> list1.remove(2)
>>> print(list1)
{8, 1, 10, 6}
>>> list1.remove(11)
>>> print(list1)
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/cc/day3/set_test.py", line 2, in <module>
    list1.remove(11)
KeyError: 11
#discard ---remove的友好版本,删除不存在的元素不会报错
>>> list1 = set([1,2,6,8,10])
>>> list1.discard(2)
>>> print(list1)
{8, 1, 10, 6}
>>> list1.discard(11)
>>> print(list1)
{8, 1, 2, 10, 6}
#pop ---随机删除
>>> list1 = set([1,2,6,8,10])
>>> list1.pop()
>>> print(list1)
{1, 2, 10, 6}  ---因集合是无序的,故随机删除某一项

2.文件操作

对文件操作流程:

  • 打开文件,得到文件句柄并赋值给一个变量
  • 通过句柄对文件进行操作
  • 关闭文件 

2.1 打开文件

#文件句柄 = open(‘文件路径‘, ‘模式‘)

文件名:yesterday

Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
The thousand dreams I dreamed

打开文件的模式有:

  • r ,只读模式【默认】
    >>> f = open("yesterday,‘r‘,encoding="utf-8")
    >>> a = f.read()
    >>> print(a)
    >>> f.close()
    Somehow, it seems the love I knew was always the most destructive kind
    不知为何,我经历的爱情总是最具毁灭性的的那种
    Yesterday when I was young
    昨日当我年少轻狂
    The taste of life was sweet
    生命的滋味是甜的
    As rain upon my tongue
    就如舌尖上的雨露
    I teased at life as if it were a foolish game
    我戏弄生命 视其为愚蠢的游戏
    The way the evening breeze
    就如夜晚的微风
    May tease the candle flame
    逗弄蜡烛的火苗
    The thousand dreams I dreamed
  • w,只写模式【不可读;不存在则创建;存在则清空内容;】
    >>> f = open("1.txt",‘w‘,encoding="utf-8")
  • a, 追加模式【可读;   不存在则创建;存在则只追加内容;】
    >>> f = open("1.txt",‘a‘,encoding="utf-8")

"+" 表示可以同时读写某个文件

  • r+, 读写【可读,可写】
    >>> f = open("1.txt",‘r+‘,encoding="utf-8")
  • w+,写读【可读,可写】
    >>> f = open("1.txt",‘w+‘,encoding="utf-8")
  • a+, 写读【可读,可写】
    >>> f = open("1.txt",‘a+‘,encoding="utf-8")

 "b"表示以字节的方式操作

  • rb  或 r+b
  • wb 或 w+b
  • xb 或 w+b
  • ab 或 a+b

 注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型

2.2 操作文件

  • read()

read() 每次读取整个文件,它通常用于将文件内容放到一个字符串变量中。然而 read() 生成文件内容最直接的字符串表示,但对于连续的面向行的处理,它却是不必要的,并且如果文件大于可用内存,则不可能实现这种处理。

>>> f = open("yesterday,‘r‘,encoding="utf-8")
>>> a = f.read()
>>> print(a)
>>> f.close()
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
The thousand dreams I dreamed
  • readlines()---适合读小文件

一次读取整个文件,象 read() 一样,readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for ... in ... 结构进行处理。

>>> f = open("yesterday","r",encoding="utf-8")#文件句柄
>>> for index,line in enumerate(f.readlines()):
>>>     if index == 9:
>>>         print(‘---我是分割线---‘)
>>>         continue
>>>     print(line.strip())
>>> f.close()
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
---我是分割线---
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
The thousand dreams I dreamed
  • readline() 

 readline() 每次只读取一行

>>> f = open("yesterday","r",encoding="utf-8")#文件句柄
>>> a = f.readline()
>>> print(a)
>>> f.close()
Somehow, it seems the love I knew was always the most destructive kind
  • 高效读取文件方法
>>> f = open("yesterday","r",encoding="utf-8")#文件句柄
>>> for line in f:
>>>      print(line.strip())
>>> f.close()
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
The thousand dreams I dreamed

2.3 管理上下文--with语句

上面我们可以看到,每次打开后都需要关闭文件,十分繁琐。。。

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

>>> with  open("yesterday","r",encoding="utf-8") as f:
>>>     .....

2.4 其他扩展

#告知当前光标的位置
>>> f = open(‘yesterday‘,‘r‘,encoding=‘utf-8‘)
>>> print(f.tell())
0
#光标回到首位
>>> f.seek(0)
#判断是否可移动 
>>> f.seekable
#判断文件是否可读
>>> f.readable()
#判断文件是否可写
>>> f.writable()
#文件描述符
f.flieno()

3.字符编码与转码

4. 函数操作

 

python学习笔记-(七)python基础--集合、文件操作&函数

标签:

原文地址:http://www.cnblogs.com/cocc/p/5746311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!