日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python文件操作,时间日期操作,collections增强,Deque(类似java的LinkedList),OrderedDict,Counter

發布時間:2024/9/27 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python文件操作,时间日期操作,collections增强,Deque(类似java的LinkedList),OrderedDict,Counter 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、文件操作的案例:

#文件操作的三種方式 #r = read 讀取文件 #w = write 寫入文件,覆蓋掉 #a = append 追加寫入,不會覆蓋原有的內容 #讀文件 f = open("E://wordcount//input//1.txt","r") data = f.read() print(data)#讀文件 一行一行的讀取 path = "E://wordcount//input//1.txt" f = open(path,"r") for line in f.readlines():print("------------->" + line)##覆蓋寫 path = "E://wordcount//input//1.txt" f = open(path,"w",encoding="utf-8") ##這種方式寫的時候,會吧別人的覆蓋了。 f.write("I love my country") f.close()##追加寫 path = "E://wordcount//input//2.txt" f = open(path,"a",encoding="utf-8") #寫一個分割 f.write("\n") f.write("I love you two") f.close()#python中的try,catch #異常處理的格式 try:path = "E://wordcount//input//2.txt"f = open(path,"r",encoding="utf-8")for line in f.readlines():print(line) finally:#不管怎樣都關閉掉文件f.close()##異常處理的簡單寫法 with open(path,"r",encoding="utf-8") as f:for i in f.readlines():print("xxxx" + i)

2、Python中的操作時間

將date類型轉化成固定的字符串
將字符串轉化成date類型

#將date轉換成字符串#使用datetime需要導入一個類庫,下面是導入類型對的方式,導包的方式還是選中 #datetime,然后Alt + Enter鍵,選擇合適的包 from datetime import datetime,timedelta #獲取當前時間 now = datetime.now() print(now)#將事件轉成成有格式類型的時間 str_time = now.strftime("%Y-%m-%d %H:%M:%S") print(str_time)##將字符串轉為Date類型 date = datetime.strptime("20170714 19:27:51","%Y%m%d %H:%M:%S") print(date)#對日期進行修改 now = datetime.now() #在現有的基礎上加8個小時 now = now + timedelta(hours=8) print(now)##加3天 now = now + timedelta(days=3) print(now)#減3天 now = now + timedelta(days=-3) print(now) #減8小時 now = now + timedelta(hours=-8) print(now)

運行結果:

3.collections增強

namedtuple:能夠tuple的每個元素起個名字
基于tuple的擴展,tuple一旦被創建無法修改,獲取tuple中的數據,只能通過角標值進行修改。

from collections import namedtupletuple = ("name","age","sex") #通過角標值獲取元素 tuple[0]#namedtuple,如果作為一個單獨的數據結構,就是每個元素都有名稱 #并且一旦創建,就無法修改。 Point = namedtuple("Point",["name","age","sex"]) p = Point("旺財","3","公") print(p.name)

運行結果:

Deque:
相當于java中的linkedlist,特征:查詢慢,刪除修改快。

from collections import deque#linked的定義 linked_list = deque(["1","2","3","4"]) #獲取linkedlist的元素 和刪除元素 item = linked_list.pop() print(linked_list) item = linked_list.popleft() print(item) print(linked_list) #添加元素,append默認在最后面添加4,appendLeft默認在最左邊添加元素 linked_list.append("4") linked_list.appendleft("1") print(linked_list)

運行結果:

deque(['1', '2', '3']) 1 deque(['2', '3']) deque(['1', '2', '3', '4'])

4 OrderedDict

from collections import OrderedDict #orderdict 按照插入的順序排序 map = OrderedDict([("key1",3),("key2",2),("key0",1)]) for item in map.items():print(item)

運行結果:

('key1', 3) ('key2', 2) ('key0', 1) from collections import Counter #計數器,計算每個字符出現的次數 #單詞計算 counter = Counter() str = "dasetgerfa" for ch in str:counter[ch] = counter[ch] + 1 print(counter)

運行結果是:

Counter({'a': 2, 'e': 2, 'd': 1, 's': 1, 't': 1, 'g': 1, 'r': 1, 'f': 1})

上面實現的邏輯相當于是:

map = dict() str = "hello" for ch in str:value = map.get(ch)if value == None:map[ch] = 1else:value += 1map[ch] = value print(map)

總結

以上是生活随笔為你收集整理的Python文件操作,时间日期操作,collections增强,Deque(类似java的LinkedList),OrderedDict,Counter的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。