我有这个for
,我想简化函数fun
lst = [{"_id":"3"}, {"_id":"1"}]
key = "1"
def fun(lst, key):
index = 0
for item in lst:
if key == item["_id"]:
return index
index += 1
return -1
fun(lst, key)
1
答案 0 :(得分:1)
您可以使用enumerate
:
def fun(lst, key):
for index, item in enumerate(lst):
if key == item["_id"]:
return index
return -1