1. 程式人生 > >python基礎(集合,文件操作)

python基礎(集合,文件操作)

但是 upd 寫入 ren eight 無序 必須 分享圖片 iss

一.集合(set)

集合:1.無序的,不重復的

   2.他裏面的元素必須是可哈希的. int str bool () ,但是它本身是不可哈希的.

   3.集合不能更改裏面的元素

   4.集合可以求交集,並集,差集,反交集等.

1.集合的創建

set1 = set({1,2,alex})
set2 = set1
print(set1,set2)
# 輸出結果:{1, 2, ‘alex‘} {1, 2, ‘alex‘}

2.集合的增(2種方式)

1.set1.add()(直接增加)
set1 = {1,3,alex,2}
# set1.add(‘taibai‘)
# print(set1)
# 輸出結果:{1, 2, 3, ‘taibai‘, ‘alex‘} 2.set1.update()(叠代增加) set1.update(abc) print(set1) # 輸出結果:{1, 2, 3, ‘c‘, ‘b‘, ‘a‘, ‘alex‘}

3.集合的刪(4種方式)

set1 = {1,3,alex,2}

1.set1.pop()   (隨機刪除,所以()內不用寫)
set1.pop()
print(set1)

2.set1.remove(2)  (刪除元素)
print(set1)

3.set.clear()   (清空集合)
set1.clear()
print
(set1) 輸出結果:set() 4.del set1(刪除集合)

4.集合的查

通過for循環查找

set1 = {1,3,alex,2}
for i in set1:
    print(i)

5.集合的其他操作

  5.1交集 ( & 或者 intersection)

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

  5.2並集 ( | 或者 union)

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

  5.3差集 ( - 或者 difference )

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

  5.4反交集 ( ^ 或者symmetric_difference)

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))

  5.5超集,子集

set1 = {1,2,3,4,5}
set2 = {4,5}
print(set1 > set2)                             #True
print(set1.issuperset(set2))

print(set2 < set1)                             #True
print(set2.issubset(set1))

這兩個相同,都是說明set1是set2的超集。set2是set1的子集

6.frozenset()

不可變集合,讓集合變成不可變類型。

set1 = {1,2,3,4,5}
s = frozenset(set1)                #frozenset({1, 2, 3, 4, 5})
print(s)

二.文件操作

1.文件操作基本流程

f = open(F:day8.txt,encoding=utf-8,mode=r)
# 變量:f f_obj,obj,file_hl file_hanlder 文件句柄          open 他是windows系統的命令
# ‘F:day8.txt‘ 文件路徑       encoding=‘utf-8‘   編碼方式                   r:只讀
content = f.read()
print(content)
f.close()        #將你這文件句柄,或者是動作關閉,節省內存

2.文件的打開模式

2.1讀

2.1.1只讀 (r,rb,r+)(5種方法)

1. f.read() 全部讀出來

f = open(asdf,r,encoding=utf-8)
content = f.read()
print(content)        #輸出結果:qweasdzxc
f.close()

2. f.read(n) n是按照字符讀出來的個數

f = open(asdf,encoding=utf-8)
content = f.read(3)
print(content)       #輸出結果:qwe
f.close()

3. readline() 按行讀出來,

f = open(asdf,encoding=utf-8)
content = f.readline()
print(content)
line1 = f.readline()
print(line1)
f.close()

4. readlines()

f = open(asdf,encoding=utf-8)
content = f.readlines()
print(content)         輸出結果:[中國\n, qweasdzxc]
f.close()

5. for 循環

f = open(asdf,encoding=utf-8)
for i in f:
    print(i)
f.close()

r+ 就是先讀後寫,在讀的基礎上加上f.write() ,在關閉文件

rb對於非文本文件,我們只能使用b模式,"b"表示以字節的方式操作(而所有文件也都是以字節的形式存儲的,使用這種模式無需考慮文本文件的字符編碼、圖片文件的jgp格式、視頻文件的avi格式)

與r的區別在於 f 中不用輸入encoding=‘ ‘,其中f.read(n)中n代表字節


bytes ---> str
# s = b‘\xe4\xb8\xb0\xe5\x8e‘.decode(‘utf-8‘)

2.2 寫write(w , w+ ,wb)

write如果沒有文件,則創建文件寫內容, 如果有文件則將原文件內容全部刪除,在寫.

f = open(asdf,w,encoding=utf-8)
content = f.write(mnbvcxy)       
f.close()

w+ 是先寫後讀,寫完之後光標在最後位置,讀不出來

wb 是寫的bytes類型。與w的區別在於 f 中不用輸入encoding=‘ ‘,其中寫入的是編碼類型,表現出的是對應的英文或者中文

2.3追加

只追加 a,ab

f = open(asdf,a,encoding=utf-8)
content = f.write(aaaa)
f.close()

ab是追加的bytes類型。與a的區別在於 f 中不用輸入encoding=‘ ‘,其中寫入的是編碼類型,表現出的是對應的英文或者中文

追加可讀 a+,a+b

f = open(asdf,a+,encoding=utf-8)
content = f.write(aaaa)
f.seek(0)
print(f.read())
f.close()

a+b是追加可讀的bytes類型。與a+的區別在於 f 中不用輸入encoding=‘ ‘,其中寫入的是編碼類型,表現出的是對應的英文或者中文

2.4文件的操作方法

常用方法:

read readable readline readlines for 循環
seek tell write writeable

技術分享圖片View Code

3.with的用法

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

4.改動文件

1)創建一個新文件.

2)讀取原文件

3)將原文件的內容通過想用的方式進行更改,並寫入新文件

4)將原文件刪除

5)將新文件重命名為原文件名

技術分享圖片
# 1,創建一個新文件.
# 2,讀取原文件.
import os
with open(log,encoding=utf-8) as f1,    open(log.bak,w,encoding=utf-8) as f2:
# 3,將原文件的內容通過你想要的方式進行更改,並寫入新文件件.
    old_content = f1.read()
    new_content = old_content.replace(alex,SB)
    f2.write(new_content)
#4,將原文件刪除.
os.remove(log)
#5,將新文件重命名原文件名.
os.rename(log.bak,log)


import os
with open(log,encoding=utf-8) as f1,    open(log.bak,w,encoding=utf-8) as f2:
    for i in f1:
        i = i.replace(alex,SB)
        f2.write(i)
os.remove(log)
os.rename(log.bak,log)
View Code

python基礎(集合,文件操作)