我有一個包含字串和整數型別混合列的 csv。(即 6 年 12 個月)。我正在嘗試找到一種將“年”和“月”轉換為僅包含月的新陣列的方法。
YrsAndMonths=np.array(['6 Years and 12 Months','7 Years and 8 Months','2 Years'])
我正在嘗試獲得類似 Months=['84','92','24'] 的輸出,不太確定如何從這里開始。
uj5u.com熱心網友回復:
有一種特定的方法應該適用于您的句子模式:
sentences = ['6 Years and 12 Months','7 Years and 8 Months','2 Years']
res = []
for x in [sentence.lower() for sentence in sentences]:
local_res = 0
if "year" in x:
year = x.split("year")
cnt = year[0]
local_res = int(cnt) * 12
if "month" in x:
month = x.split("month")[0].split("and")[1].strip()
cnt = month
local_res = int(cnt)
res.append(local_res)
print(res)
uj5u.com熱心網友回復:
這有點超出了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497495.html
下一篇:如何洗掉字串末尾的所有逗號?
