python基础七

集合:


无序的

不重复

里面元素必须可哈希的,但本身是不可哈希

集合不能更改

集合可以求交集、并集、差集、反交集

去重:①用算法做

          ②转换成集合,再转换回来

   

li=[1,1,2,3,3,4,4,5,5,5,6]
li = set(li)
li = list(li)
print(li)
#输出列表:[1, 2, 3, 4, 5, 6]

创建集合:

#
set1={"",""}
#
set1=set({})

#添加一个元素
set1 = {1,2,3,4,5}
set1.add(678)
print(set1)
#输出:{1, 2, 3, 4, 5, 678}
-------------------------------------
#迭代添加
#列表
set1 = {1,2,3,4,5}
set1.update([6,7,8])
print(set1)
#输出:{1, 2, 3, 4, 5, 6, 7, 8}
 

#字符串
set1.update("678")
 print(set1)
#输出:{1, 2, 3, 4, 5, '8', '6', '7'}

删:

set1 = {"a",1,2,3,4,5,"b"}
set1.remove('a')  # 删除一个元素
print(set1)

set1.pop()  # 随机删除一个元素
print(set1)

set1.clear()  # 清空集合
print(set1)

del set1  # 删除集合
print(set1)

: for循环

交集: 

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 & set2)  # {4, 5}
print(set1.intersection(set2))  # {4, 5}

并集:

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2)  # {1, 2, 3, 4, 5, 6, 7}

print(set2.union(set1))  # {1, 2, 3, 4, 5, 6, 7}

差集:

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 - set2)  # {1, 2, 3}
print(set1.difference(set2))  # {1, 2, 3}

反交集:

set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 ^ set2)  # {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2))  # {1, 2, 3, 6, 7, 8}

子集与超集:

set1 = {1,2,3}
set2 = {1,2,3,4,5,6}

print(set1 < set2)
print(set1.issubset(set2))  # 这两个相同,都是说明set1是set2子集。

print(set2 > set1)
print(set2.issuperset(set1))  # 这两个相同,都是说明set2是set1超集。

frozenset不可变集合,让集合变成不可变类型:

set1 = {1,2,3,4,5}
s = frozenset(set1)
print(s,type(s))
#输出:frozenset({1, 2, 3, 4, 5}) <class 'frozenset'>

 文件操作:

#1. 打开文件,得到文件句柄并赋值给一个变量 
f 变量:f f_obj,obj,file_hl file_hanlder 文件句柄
f=open('a.txt','r',encoding='utf-8') #默认打开模式就为r #2. 通过句柄对文件进行操作 content=f.read() print(content) #3. 关闭文件 f.close()

读:

#r模式
f = open('log','r',encoding='utf-8')
content = f.read()
print(content)
f.close()
输出:中国abcd
-----------------
#rb模式
f = open('log','rb')
content = f.read()
print(content)
f.close()
#输出:b'xe4xb8xadxe5x9bxbd abcd'

只读的五种模式:

①f.read()                    全部都出来
②f.readline()                按行读
③f.read(n)                   r模式:读n个字符      rb模式:读n个字节
④f.readlines()               每一行作为一个元素,放在列表里 
⑤for i in f:                 一行一行读,不占内存
     print()

bytes 转换成str:

s = b"xe4xb8....".decode("utf-8)

print(s)

写:

如果没有文件,则创建文件写内容

如果有文件,将原内容删除,再写

f = open('log','w',encoding="utf-8")
content = f.write("abc")
f.close()
------------------------
#wb模式,需要把写的转换成utf-8
f = open('log1','wb')
content = f.write("abc".decode('utf-8'))
f.close()

追加:a  ab

f = open('log','a',encoding='utf-8')
f.write('defg紧跟其后')
f.close()
#原来log文件里 是 abc   追加后变成:abcdefg紧随其后

读写: r+       r+b

             先读后写

f = open('log','r+',encoding='utf-8')
print(f.read())
f.write('efgh')
f.close()
#都出来的没有“efgh”  因为先读,后写

写读:w+  w+b

f = open('log','w+',encoding='utf-8')
f.write('efgh')
f.seek(0)  #移动光标
print(f.read())
f.close()
#把原文件删除,再写,然后读,由于光标在最后,蓄意需要移动光标才能读出来

追加可读:a+    a+b


常用方法补充:

readable()判断是否是只读

writeable()判断是否是只写

tell()获取光标的位置       按字节获取

seek()移动光标               按字节调整

truncate(3)截取前面的字节   按字节

用with自动关闭,不写close,一个with可以操作多个文本:

with open('log','r',encoding='utf-8') as f1,
        open('log1','r',encoding='utf-8') as f2:
    print(f1.read())
    print(f2.read())

改动文件

                  ①创建一个新文件

      ②读原文件

      ③将原来文件内容,通过你想的方式更改,并写入新的文件

                   ④删除文件

       ⑤新文件命名

原文地址:https://www.cnblogs.com/pygg/p/8385818.html