#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Compare 2 listst


    Hello,

    I'm new to the python programming, and I have a question.
    I have to write a program that prints a receipt for a restaurant. The input is a list which looks like:

    product1
    product3
    product8
    ....

    In the other input file there is a list which looks like:

    product1 coffee 5,00
    product2 bread 7,50
    ....

    So I need to compare the first list to the second and print the receipt with prices and a total price.

    It doesn't look that hard but I just don't know how or where to start. Can somebody help me?

    Thank you
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Hello there,


    You can try the following which will print only those lines in your file whose product name is in your list.

    Code:
    def printProductInfo(param_filePath, param_prodList):
            
        with open(param_filePath, 'r') as prodFile:
            for productInfo in prodFile:
                # the first column in your file is the product code
                productCode = productInfo.split()[0] 
                
                # check to see whether the product in the current
                # line in the file is in the list of products
                if productCode in param_prodList:
                    print (productInfo)
    
    
    prodList = ['product1', 'product2', 'product6', 'product7']
    printProductInfo('./data.txt', prodList)

    This works only if we assume that product code is always the first column in your file.
    Also I assumed that your file was space/tab delimited. If you use other delimiter such as comma, semicolon, pipe, etc.
    then you will need to define properly the split method argument in
    Code:
    productCode = productInfo.split()[0]

    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Yes, this helped me a lot! Thank you very much.

    Is there also a way to erase the "product2" in the printed line?
    so it prints:

    bread 7,50

    instead of

    product2 bread 7,50

    Thank you!
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by dagendy
    ... Is there also a way to erase the "product2" in the printed line? ...
    No problem!

    Code:
    def printProductInfo(param_filePath, param_prodList):
            
        with open(param_filePath, 'r') as prodFile:
            for productInfo in prodFile:
                # the first column in your file is the product code
                productCode = productInfo.split()[0] 
                
                # check to see whether the product in the current
                # line in the file is in the list of products
                if productCode in param_prodList:
                    print (" ".join(productInfo.split()[1:]))
    
    
    prodList = ['product1', 'product2', 'product6', 'product7']
    printProductInfo('./data.txt', prodList)

    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo