Solved

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

Posted on 2006-07-06
5
201 Views
Last Modified: 2010-04-16
Hi,

This question is again related to a previous question:

http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21905315.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
0
Comment
Question by:JohnAutoSales
  • 3
  • 2
5 Comments
 

Author Comment

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

[[GarageName, Make1Model1, Make2Model2, Make3Model3] , [GarageName, Make1Model1, Make2Model2] ...... ]
0
 
LVL 28

Expert Comment

by:pepr
ID: 17061523
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
0
 

Author Comment

by:JohnAutoSales
ID: 17061656
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
0
 

Author Comment

by:JohnAutoSales
ID: 17062950
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
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 17069017
O.K., let's modify the garage class. The property of having the kinds of cars is not related directly to any vehicle. It is rather related to the garage. Let's call the make+model the signature of the car. The method rebuildCarSignatures() was extracted from the previous uniqueCarTypesAsListOfStrings(). It is not optimized and it is called in the reimplemented uniqueCarTypesAsListOfStrings(), and also in the new uniqueCarTypesAsFormatedString() and in hasAtLeastNSignatures(). The last method is called for testing whether the garage has at least given number of car signatures (i.e. of cars of different make/model identifications). See also the fragments of the body of the  script that uses the methods:

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

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

    def rebuildCarSignatures(self):
        """Creates again the set of car signatures present in the garage."""

        self.__signatures.clear()
        for vehicle in self.vehicles:
            self.__signatures.add(vehicle.make + '|' + vehicle.model)

    def uniqueCarTypesAsListOfStrings(self):
       
        # Build the set of present cars signatures.
        self.rebuildCarSignatures()

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

        return lst
 

    def uniqueCarTypesAsFormatedString(self):
       
        # Build the set of present cars signatures.
        self.rebuildCarSignatures()

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

        return '\n'.join(lst)


    def hasAtLeastNSignatures(self, n):
        self.rebuildCarSignatures()
        return len(self.__signatures) >= n

[...snip...]





print '-' * 70
print 'Vehicle types in garages -- as one string.'
for garage in garages:
    print 'Garage:', garage.name
    print garage.uniqueCarTypesAsFormatedString()
    print
print


print '-' * 70
print 'Garages with at least 2 car types.'
for garage in garages:
    if garage.hasAtLeastNSignatures(2):
        print 'Garage:', garage.name
        print garage.uniqueCarTypesAsFormatedString()
        print
print
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python output problem 10 49
Python mysql Insert data error 3 64
Python Assistance 7 31
linux date/time setting via python 6 37
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now