给定值列表,请删除首次出现的值 [英] Given a list of values remove first occurrence

查看:56
本文介绍了给定值列表,请删除首次出现的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def drop dest(routes,location):
    for i in range(len(routes)):
        if routes[i] == location:
              routes.remove(routes[i])
    return routes

我正在使用给定列表作为
routes = [(3,2),(2,4),(5,5),(2,4)]的函数定义,并且说我只想删除(2,4)的第一个出现值.我对如何执行此操作有些困惑,因为我确实删除了该值,但同时也删除了给定的其他值.我只想删除第一个给定值的地方.

I am using a function definition given a list as
routes = [(3,2),(2,4),(5,5),(2,4)], and say I just want to remove the first occurrence value of (2,4). I am a little confused in how to do this because I do remove the value but I also remove the other value given. Where I just want to remove the first given value.

推荐答案

很简单,使用list.remove.

>>> routes = [(3,2),(2,4),(5,5),(2,4)]
>>> routes.remove((2,4))
>>> routes
[(3, 2), (5, 5), (2, 4)]

这篇关于给定值列表,请删除首次出现的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆