我試圖通過列表循環以找到匹配除最後一個字符以外的文件名,並將匹配串接成一個字符串。Python循環遍歷匹配字符串的長列表並連接
我遇到的困難是每個文件名有不同的匹配數量,所以可能有一個文件沒有匹配,一個文件有兩個匹配,三個或四個。
我正在使用'skip'變量來嘗試並通過迭代已經匹配的循環來避免重複。
我認爲問題來自於我使用'i'變量從列表中獲取元素,但我不確定。
正如你或許可以告訴我是新來的Python和編程,並有一個嚴重的缺陷,在我的邏輯,我看不到!如果有什麼不清楚的地方,我會盡我所能解釋,任何幫助將不勝感激。
reader = [34113751IHF.jpg, 34113751IHR.jpg, 34136676OTD.jpg, 34136676OTF.jpg, jpg34136676OTR.jpg, 34136676OTF.jpg, 34136676OTR.jpg, 34139933EDD.jpg, 34139933EDF.jpg, 34144626KXF.jpg, 34144626KXR.jpg]
iterable = iter(reader)
skip = 0
for i, j in enumerate(iterable):
firstURL = str(j)[2:-2]
firstShorter = str(reader[i+1])[2:-3]
secondURL = str(reader[i+1])[2:-2]
secondShorter = str(reader[i+1])[2:-3]
if firstShorter == secondShorter:
toWrite = firstURL + ".jpg|" + secondURL + ".jpg"
thirdURL = str(reader[i+2])[2:-2]
thirdShorter = str(reader[i+2])[2:-3]
skip = 2
if secondShorter == thirdShorter:
toWrite += "|" + thirdURL + ".jpg"
fourthURL = str(reader[i+3])[2:-2]
fourthShorter = str(reader[i+3])[2:-3]
skip = 3
if thirdShorter == fourthShorter:
toWrite += "|" + thirdURL + ".jpg"
fifthURL = str(reader[i+4])[2:-2]
fifthShorter = str(reader[i+4])[2:-3]
skip = 4
else:
toWrite = firstURL + ".jpg"
skip = 1
[iterable.__next__() for x in range(skip)]
你能寫出你期望的輸出嗎?很難理解你的問題。你想要一個像這樣的列表:[「34113751IHF34113751IHR」,「34136676OTD34136676OTF」,...]? –