我有一個清單。我想從最后一個完成的句子中獲得價值。像最后一個“。” 不是第一,第二或第三。
lst = ["1st", "2nd", "?", "3rd", ".", "4th", "5th", "6th", ".", "7th", "8th"]
我正在努力實作
Output: 1st 2nd? 3rd. 4th 5th 6th.
如何通過 for 回圈而不是索引來做到這一點?索引不起作用,因為有無限的串列,并且每次都是最后一個“。” 位置可以不同。
uj5u.com熱心網友回復:
如果我正確地回答了您的問題:
lst = ["1st", "2nd", "?", "3rd", ".", "4th", "5th", "6th", ".", "7th", "8th"]
output = []
start_index = 0
for end_index, char in enumerate(lst):
if char == ".":
output.extend(lst[start_index:end_index 1])
start_index = end_index 1
print(" ".join(output))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432193.html
標籤:python-3.x for循环 if 语句
上一篇:`print()`是如何作業的?
