0

I am still learning Python and I have a problem. If my question isn't that clear, please be nice!

Is it possible that while using a list, I can delete an object from the list if only one object matches

So for example:

driver.addDriver(Driver("Ben", "BBB"))                 
driver.removeDriver("Ben", "123")

Can I remove the driver name and print as None while still showing the vehicle number. Thanks.

class Driver:
    def __init__(self, name, vehNo):
        self._name = name
        self._vehNo = vehNo

    @property
    def name(self):
        return self._name

    @property 
    def vehNo(self):
        return self._vehNo
    @vehNo.setter
    def vehNo(self, newVehNo):
        self._vehNo = newVehNo

    def __str__(self):
        return 'Driver Name: {} Vehicle Number: {}'.format(self._name, self._vehNo)


class TransportServices:
    def __init__(self):

    self._drivers   = []



    def searchDriver(self, name = None, vehNo = None):
        for d in self._drivers:
            if d.name == name and d.vehNo == vehNo:
                return d

        return None


#############################################################################

    def addDriver(self, driver):
        d = self.searchDriver(driver.name)
        if d is None:
            self._drivers.append(driver)
            return True
        else:
            return False

#############################################################################

    def removeDriver(self, name = None, vehNo = None):
        d = self.searchDriver(name, vehNo)
        if d is None:
            return False

        else:
            self._drivers.remove(d)



#############################################################################   

    def __str__(self):

        drivers = [str(d) for d in self._drivers]
        return "{} ".format('\n'.join(drivers))



def main():


    driver = TransportServices()
    driver.addDriver(Driver("Alan", "AAA"))
    driver.addDriver(Driver("Ben", "BBB"))
    driver.removeDriver("Ben", "123")



    print(driver)
main()
6
  • Your code seems okay at first glance, Are you facing any specific issue? Commented Apr 2, 2019 at 17:16
  • i could delete the whole list, but I could not figure out how to delete only either the driver name or vehicle number. Commented Apr 2, 2019 at 17:17
  • I'm not sure what you mean by "delete either the driver name or vehicle number". Your array is made of objects that contain both of those things as attributes. If you remove the object, both of those attributes will obviously be gone too. Commented Apr 2, 2019 at 17:20
  • what i meant was, can I just delete the driver name, and still be able to print out the vehicle number or vice versa. Commented Apr 2, 2019 at 17:21
  • What is your desired relationship between driver names and vehicle numbers? Right now you have a list of (driver, vehicle) pairs, but it sounds like you might want something more complex; perhaps two mappings, one of (driver -> list of vehicles) and one of (vehicle -> list of drivers)? Commented Apr 2, 2019 at 17:22

1 Answer 1

1

Basically what you are looking for is not deleting the object but updating it.

You can update the corresponding object as below:

for driver in self.drivers:
    if driver.name == 'Bob': # or  driver vehNo == 'BBB'
        driver.name = None

Also for your case, you could rather use a dictionary which is the same as a hash map in Java.

You can do some thing like below:

self.drivers = {}

self.driver['vehicle Num'] = theDriverObject

so that when you need to access or update you can do it instantly i.e. O(1) without having to loop through all the drivers.

Sign up to request clarification or add additional context in comments.

1 Comment

will try out. I was told to use a list for this although i agree using a dict will be much easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.