Given a list of values remove first occurrence

前端 未结 3 1910
旧巷少年郎
旧巷少年郎 2021-01-19 03:39
def drop dest(routes,location):
    for i in range(len(routes)):
        if routes[i] == location:
              routes.remove(routes[i])
    return routes
         


        
3条回答
  •  囚心锁ツ
    2021-01-19 04:13

    if that is your code and needs to be in a loop and removed only once I would do it like that:

    def drop_dest(routes,location):
         flag = 1
         for i in range(len(routes)):
              if routes[i] == location and flag == 1:
                  routes.remove(routes[i])
                  flag = 0
         return routes´
    

提交回复
热议问题