![](/img/trans.png)
[英]Binary search to find last element in sorted list that is less then specific value
[英]How to take last element with a specific value in a list
我有一個具有“類型”屬性的 Document 對象。 我需要循環一個 Document 列表,對於每個 document.type ==“OTHER”,我必須采用列表中的最后一個元素,即“MIC”document.type。 我怎樣才能輕松做到這一點?
我已經反轉列表並循環遍歷它如果新元素的類型如果不是某些類型之一,則它被視為其他類型並打破循環,因為它最初是這種類型的最后一個
types = [float, str]
texts = ['hello', 3, 2.5, 'world', 7, 8, 1.4]
texts = texts[::-1]
for i in texts:
ty = type(i)
if ty in types:
pass
else:
other = ty
print(i)
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.