集合是一个无序的,不重复的数据组合,它的主要作用如下:
- 1 s = set([1,4,5,7,3,8,7,9,6]) #创建一个数值集合
- 2
- 3 t = set("Hello") #创建一个唯一字符的集合
- 4
- 5
- 6 a = t | s # t 和 s的并集
- 7
- 8 b = t & s # t 和 s的交集
- 9
- 10 c = t – s # 求差集(项在t中,但不在s中)
- 11
- 12 d = t ^ s # 对称差集(项在t或s中,但不会同时出现在二者中)
- 13
- 14
- 15
- 16 基本操作:
- 17
- 18 t.add('x') # 添加一项
- 19
- 20 s.update([10,37,42]) # 在s中添加多项
- 21
- 22
- 23
- 24 使用remove()可以删除一项:
- 25
- 26 t.remove('H')
- 27
- 28
- 29 len(s)
- 30 set 的长度
- 31
- 32 x in s
- 33 测试 x 是否是 s 的成员
- 34
- 35 x not in s
- 36 测试 x 是否不是 s 的成员
- 37
- 38 s.issubset(t)
- 39 s <= t
- 40 测试是否 s 中的每一个元素都在 t 中
- 41
- 42 s.issuperset(t)
- 43 s >= t
- 44 测试是否 t 中的每一个元素都在 s 中
- 45
- 46 s.union(t)
- 47 s | t
- 48 返回一个新的 set 包含 s 和 t 中的每一个元素
- 49
- 50 s.intersection(t)
- 51 s & t
- 52 返回一个新的 set 包含 s 和 t 中的公共元素
- 53
- 54 s.difference(t)
- 55 s - t
- 56 返回一个新的 set 包含 s 中有但是 t 中没有的元素
- 57
- 58 s.symmetric_difference(t)
- 59 s ^ t
- 60 返回一个新的 set 包含 s 和 t 中不重复的元素
- 61
- 62 s.copy()
- 63 返回 set "s"的一个浅复制
对文件操作流程
打开文件的模式有:
详细信息可以参考 http://www.cnblogs.com/zhangqigao/articles/6484403.html
"+" 表示可以同时读写某个文件
"U" 表示在读取时,可以将 \r \n \r\n 自动转换成 \n (与 r 或 r+ 模式同使用)
"b" 表示处理二进制文件(如:FTP 发送上传 ISO 镜像文件,linux 可忽略,windows 处理二进制文件时需标注)
现有文件如下:
yesterday
- 1 Somehow, it seems the love I knew was always the most destructive kind
- 2 不知为何,我经历的爱情总是最具毁灭性的的那种
- 3 Yesterday when I was young
- 4 昨日当我年少轻狂
- 5 The taste of life was sweet
- 6 生命的滋味是甜的
- 7 As rain upon my tongue
- 8 就如舌尖上的雨露
- 9 I teased at life as if it were a foolish game
- 10 我戏弄生命 视其为愚蠢的游戏
- 11 The way the evening breeze
- 12 就如夜晚的微风
- 13 May tease the candle flame
- 14 逗弄蜡烛的火苗
- 15 The thousand dreams I dreamed
- 16 我曾千万次梦见
- 17 The splendid things I planned
- 18 那些我计划的绚丽蓝图
- 19 I always built to last on weak and shifting sand
- 20 但我总是将之建筑在易逝的流沙上
- 21 I lived by night and shunned the naked light of day
- 22 我夜夜笙歌 逃避白昼赤裸的阳光
- 23 And only now I see how the time ran away
- 24 事到如今我才看清岁月是如何匆匆流逝
- 25 Yesterday when I was young
- 26 昨日当我年少轻狂
- 27 So many lovely songs were waiting to be sung
- 28 有那么多甜美的曲儿等我歌唱
- 29 So many wild pleasures lay in store for me
- 30 有那么多肆意的快乐等我享受
- 31 And so much pain my eyes refused to see
- 32 还有那么多痛苦 我的双眼却视而不见
- 33 I ran so fast that time and youth at last ran out
- 34 我飞快地奔走 最终时光与青春消逝殆尽
- 35 I never stopped to think what life was all about
- 36 我从未停下脚步去思考生命的意义
- 37 And every conversation that I can now recall
- 38 如今回想起的所有对话
- 39 Concerned itself with me and nothing else at all
- 40 除了和我相关的 什么都记不得了
- 41 The game of love I played with arrogance and pride
- 42 我用自负和傲慢玩着爱情的游戏
- 43 And every flame I lit too quickly, quickly died
- 44 所有我点燃的火焰都熄灭得太快
- 45 The friends I made all somehow seemed to slip away
- 46 所有我交的朋友似乎都不知不觉地离开了
- 47 And only now I'm left alone to end the play, yeah
- 48 只剩我一个人在台上来结束这场闹剧
- 49 Oh, yesterday when I was young
- 50 噢 昨日当我年少轻狂
- 51 So many, many songs were waiting to be sung
- 52 有那么那么多甜美的曲儿等我歌唱
- 53 So many wild pleasures lay in store for me
- 54 有那么多肆意的快乐等我享受
- 55 And so much pain my eyes refused to see
- 56 还有那么多痛苦 我的双眼却视而不见
- 57 There are so many songs in me that won't be sung
- 58 我有太多歌曲永远不会被唱起
- 59 I feel the bitter taste of tears upon my tongue
- 60 我尝到了舌尖泪水的苦涩滋味
- 61 The time has come for me to pay for yesterday
- 62 终于到了付出代价的时间 为了昨日
- 63 When I was young
- 64 当我年少轻狂
基础操作:
1、文件读取
- 1 f = open("yesterday","r",encoding="utf-8")
- 2 first_line = f.readline()
- 3 print("read first line:",first_line)
- 4 print("我是分割线".center(50,'-')) #打印内容居中,其他'-'
- 5 d = f.read() #读取文件中剩余内容
- 6 print(d)
- 7 f.close()
2、文件覆盖写入
3、追加写入
- 1 f = open("yesterday3","a",encoding="utf-8")
- 2 f.write("我是新的数据,\n来读取我看看")
- 3 f.close()
4、读取多行文件
- 1 #读文件前5行
- 2 f = open("yesterday","r",encoding="utf-8")
- 3 for i in range(5):
- 4 print(f.readline().strip())
5、有条件部分读取
方法一:(内存缓存)
- 1 #读取全部文件,到第十行就结束
- 2 f = open("yesterday","r",encoding="utf-8")
- 3 for index,line in enumerate(f.readlines()):
- 4 if index == 9:
- 5 print("-----我是分割线-----")
- 6 break
- 7 print(index,line.strip())
方法二:(高效读取)for line in f:
- 1 f = open("yesterday","r",encoding="utf-8")
- 2 count = 0
- 3 for line in f: #文件逐行读取,效率最高,此时line已不为列表,需要重新计数
- 4 if count == 9:
- 5 print("---我是分割线---")
- 6 count +=1
- 7 break
- 8 print(line.strip())
- 9 count +=1
6、读取光标位置 tell()
- 1 f = open("yesterday","r",encoding="utf-8")
- 2 print(f.tell())
- 3 print(f.readline().strip())
- 4 print(f.tell())
7、移动光标位置,光标返回 0 的位置
- 1 f = open("yesterday","r",encoding="utf-8")
- 2 print(f.tell())
- 3 print(f.readline().strip())
- 4 print(f.tell())
- 5 f.seek(0)
- 6 print(f.tell())
8、打印文件编码
- 1 f = open("yesterday","r",encoding="utf-8")
- 2 print(f.encoding)
9、打印读取文件的 IO 接口
- 1 f = open("yesterday","r",encoding="utf-8")
- 2 print(f.fileno())
10、强制刷新,写数据时,写的数据不想存内存中缓存中,而是直接存到磁盘上,需要强制刷新。flush()
- 1 >>> f = open("yesterday2","w",encoding="utf-8")
- 2 #这时'hello word'在缓存中
- 3 >>> f.write("hello word")
- 4 10
- 5 #强刷到磁盘上
- 6 >>> f.flush()
11、进度条,体会 flush 的作用
- 1 import sys,time
- 2
- 3 for i in range(50):
- 4 sys.stdout.write(">")
- 5 sys.stdout.flush()
- 6 time.sleep(0.5)
来源: http://www.bubuko.com/infodetail-1980399.html