Link to home
Start Free TrialLog in
Avatar of JohnAutoSales
JohnAutoSales

asked on

Checking the members of a list and appending the list if there is new data

Hi,

This question is again related to a previous question:

https://www.experts-exchange.com/questions/21905315/Getting-parent-node-attribute-values-from-XML-files.html


I can make a list of the following structure:
[[GarageName, Make, Model] , [GarageName, Make, Model] , [GarageName, Make, Model] .....]


I need to be able to compare all of the remainder vehicles to those of this list.  If a particular vehicle make or model already appears in any given garage, then the garage should not be appended.  If however, the make or the model is not in the list for a particular garage, then they should be appended to the relevant garage list within the big list.

I think that maybe the best way to represent this data would in a dictionary with inner lists, i.e.,

{"GarageName":[["Make1","Model1"] , ["Make2","Model2"] , ["Make3","Model3"] , ["Make4","Model4"]] }


Your thoughts?

John


Hope you can help,
John
Avatar of JohnAutoSales
JohnAutoSales

ASKER

If it was just kept in a straightforward list, it would be better. Ideally like this:

[[GarageName, Make1Model1, Make2Model2, Make3Model3] , [GarageName, Make1Model1, Make2Model2] ...... ]
Avatar of pepr
I suggest first to enlight the basic idea. What is the purpose of the list? In the context of the mentioned solution from the related question... Do you want to make another list for garage that would represent present make/model characteristic in the sense "Is there at least Toyota Yaris in the garage?" rather than "There are 3 Toyota Yaris in the garage". Or do you want to show to the customer the list of present types of cars in each garage? How the list is to be used?

It is important to clarify that, because it is usually better to implement the purpose, not to fit the solution to the previously designed data structures that may not reflest the final needs of the solution.

I guess that it would be easier to create another method of the Garage class. Calling the method of the garage object would return the required info, possibly formated in some human-readable form.

For example, the previous Gerage class could be given the extra method uniqueCarTypesAsListOfStrings():

class Garage(object):
    """Class representing one garage with zero or more vehicles."""

    def __init__(self, name):
        self.name = name
        self.vehicles = set()

    def uniqueCarTypesAsListOfStrings(self):
       
        # Build the set of present cars signatures.
        make_type = set()
        for vehicle in self.vehicles:
            mt = vehicle.make + '|' + vehicle.model
            make_type.add(mt)


        # Format the set of signatures as list of strings.
        lst = []
        for mt in make_type:
            m, t = mt.split('|')
            lst.append('    ' + m.capitalize() + ' ' + t.capitalize())

        return lst


... and the result could be obtained by the code:

print '-' * 70
print 'Vehicle types in garages.'
for garage in garages:
    print 'Garage:', garage.name
    print '\n'.join(garage.uniqueCarTypesAsListOfStrings())
    print
print
Hi,

Sorry for not making the problem clearer.  Yes, I would like to be able to show the customers all the makes in the same garage, and I'll also need to have the data in the following format for later:

[[GarageName, Make1Model1, Make2Model2, ....] , [GarageName, Make1Model1, Make2Model2, ....] , .........]


Thanks,
John
Hi again,

Your answer pepr has pretty much solved my problem.  I'm happy to give you the points.  Just one more thing though:

I'm trying to apply another function to members of this new list (lst in your code), but only for list members that have two or more makes and models.

E.g. in the lst:

[[Wood Road, Toyota Yaris, Ford Mondeo] , [John Street, Nissan Note]]

only the Wood Road inner list would be affected since it has two or more makes and models.


I've been trying to figure out an if statement for this, but can't put my finger on it.



Thanks,
John
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial