我在Advent of Code: Day 2上工作,Im在列表工作時遇到問題。我的代碼需要一個字符串,例如2x3x4,並將其分割成一個列表。然後它檢查列表中的'x',並將其刪除並將該值提供給計算所需區域的方法。問題是,在刪除'x'之前,我需要找出'x'前面是否有兩個數字併合並它們,以計算兩位數字。我研究過正則表達式,但我認爲我沒有正確使用它。有任何想法嗎?在Python中比較和組合列表項目
def CalcAreaBox(l, w, h):
totalArea = (2*(l*w)) + (2*(w*h))+ (2*(h*l))
extra = l * w
toOrder = totalArea + extra
print(toOrder)
def ProcessString(dimStr):
#seperate chars into a list
dimStrList = list(dimStr)
#How to deal with double digit nums?
#remove any x
for i in dimStrList:
if i == 'x':
dimStrList.remove(i)
#Feed the list to CalcAreaBox
CalcAreaBox(int(dimStrList[0]), int(dimStrList[1]), int(dimStrList[2]))
dimStr = "2x3x4"
ProcessString(dimStr)
看看這個:http://www.tutorialspoint.com/python/string_split.htm –