Reputation: 1
I am very new to Python and programming in general. I have found many useful items on this site, but I have also confused myself in regards to what the best way to accomplish what I need.
Simply put, I have 2 csv files, RemoteLA and Master. Each line item contains Order#, LastName, FirstName, MI, Account#. I have to find the items from RemoteLA that are not in Master file. My main concern is matching/verifying Order# first, then Account#, then name(s). There are instances where I can have two of the same order#'s but different names and Account#'s, which is ok as long as it is listed the same in the Master file. Lastly, if the RemoteLA order number is in the Master file, but the account number is listed incorrectly, I would like to print it in a different output than if the Order# wasn't in the Master file at all. Below are my simplified lists.
RemoteLA = [['100', 'JACKSON', 'CHRIS', 'D', '12344'], ['110', 'SMITH', 'JANET', 'L' '1223'], ['120', 'STONE', 'MAX', 'W', '1233']]
Master = [['100', 'JACKSON', 'CHRIS', 'D', '1234'], ['90', 'BARST', 'JOEY', 'D', '1344'], ['80', 'NORDON', 'BEV', 'A', '1122'], ['120', 'STONE', 'MAX', 'W', '1233']]
Like I said, I have tried many things using sets, list of tuples, etc. My last 'try' was using a while loop as follows-I was just trying to see if I could get the results I wanted, but it appears I am still doing something wrong.
i=0
while i < len(RemoteLA):
j = 0
while j < len(Master):
if RemoteLA[i][0] == Master[j][0]:
if RemoteLA[i][1] == Master[j][1]:
if RemoteLA[i][2] == Master[j][2]:
print('All match')
elif RemoteLA[i][2] == '9999':
pass
else:
print('Account number does not match')
else:
print('Name does not match')
else:
print('Order number does not match')
j = j + 1
i = i + 1
Any help or push in the right direction would be very much appreciated. Sorry for the wordiness. Thank you.
Upvotes: 0
Views: 434
Reputation: 9620
If I understand, account numbers are unique. Therefore use them as dictionary keys. Here, let's reorganize your data:
def orderSummary(lst):
info = collections.defaultdict(dict)
for item in lst:
acct = item[4]
orderinfo = info[acct]
ordernum = item[0]
if ordernum in orderinfo:
print "duplicate order number"
else:
orderinfo[ordernum] = tuple(item[1:4]) #the name
return info
remote = orderSummary(RemoteLA)
master = orderSummary(Master)
Now we are ready to check the remote against the master:
def checkRemoteAgainstMater(remote,master):
for acct,info in remote.items():
masterinfo = master.get(acct,None)
if masterinfo is None:
print "bad account number {}".format(acct)
else:
for order in info:
if order not in masterinfo:
print "Master missing order"
elif info[order] != masterinfo[order]:
print "name mismatch"
checkRemoteAgainstMater(remote,master)
hth.
Upvotes: 1
Reputation: 39406
A much shorter (although just as inefficient) would be:
import itertools
for master,remote in itertools.product(Master, RemoteLA):
if all(r == m for r,m in zip(master, remote)):
print "Match", master
A more efficient version would be to sort the lists first (O(nlogn)), in order to have a O(n) comparison.
Upvotes: 0