資料來源: https://docs.python.org/3/library/stdtypes.html#str.find
![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/11/20221122100657_32.png)
data.txt:
I love you all. But you do love love me at all.\nYou always love Python.
程式碼:
filePath = r”C:\Python\myfind\data.txt”
def keywordIndexFromFile(patterns : str, filePath : str) -> list:
with open(filePath,”r”) as f:
dataStr = f.read()
#print(dataStr)
lst=[]
idx = dataStr.find(patterns)
#lst=[ idx ]
while idx != -1:
#idxTmp = dataStr.find(patterns,idx+1)
lst.append( idx )
idx = dataStr.find(patterns,idx+1)
return lst
print(keywordIndexFromFile(“love”,filePath))
![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/11/20221122101109_3.png)
參考解答:
![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/11/20221122101727_51.png)
詞頻計算:
![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/11/20221122111312_19.png)
# -*- coding: utf-8 -*-
from typing import Tuple
filePath = r”C:\Python\myfind\data.txt”
def keywordIndexFromFile(patterns : str,
filePath : str) -> Tuple[list[int],float]:
with open(filePath,”r”) as f:
dataStr = f.read()
#print(dataStr)
lst=[]
idx = dataStr.find(patterns)
#lst=[ idx ]
while idx != -1:
#idxTmp = dataStr.find(patterns,idx+1)
lst.append( idx )
idx = dataStr.find(patterns,idx+1)
lstAll = dataStr.split(” “)
#print(lstAll , len(lstAll))
termFrequency = len(lst) / len( lstAll )
return lst, termFrequency
lst, termFrequency = keywordIndexFromFile(“love”,filePath)
print(“Key word index:”,lst)
print(“詞頻: %.2f %%” %(termFrequency*100) )
![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/11/20221122111354_84.png)
參考解答:
![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/11/20221122111722_6.png)
改用**dict當作輸入參數:
![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/11/20221123083724_5.png)

![Python: 如何用numpy.ndarray的reshape 將3D array轉為2D array,再轉為pandas.DataFrame? arr.reshape( arr.shape[0] * arr.shape[1] , -1) Python: 如何用numpy.ndarray的reshape 將3D array轉為2D array,再轉為pandas.DataFrame? arr.reshape( arr.shape[0] * arr.shape[1] , -1)](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230320082325_85.png?quality=90&zoom=2&ssl=1&resize=350%2C233)





![Python如何做excel的樞紐分析? groupbyObj = df.groupby([‘A’, ‘B’]) ; groupbyObj.apply() 跟 groupbyObj.agg() 差異為何? result = groupbyObj .apply( function(df) -> Series ) ; result_agg = groupbyObj .agg( [‘mean’, ‘std’] ) ; aggfunc(Series) -> float Python如何做excel的樞紐分析? groupbyObj = df.groupby([‘A’, ‘B’]) ; groupbyObj.apply() 跟 groupbyObj.agg() 差異為何? result = groupbyObj .apply( function(df) -> Series ) ; result_agg = groupbyObj .agg( [‘mean’, ‘std’] ) ; aggfunc(Series) -> float](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230327140158_46.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python:如何用pandas.concat() 合併兩個DataFrame並重置index? pd.concat([df1, df2]) .reset_index(drop=True) ; pd.concat([df1, df2], ignore_index=True) Python:如何用pandas.concat() 合併兩個DataFrame並重置index? pd.concat([df1, df2]) .reset_index(drop=True) ; pd.concat([df1, df2], ignore_index=True)](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230311123232_5.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

近期留言