I'm still only a few months into python, so please excuse the ugly code. I have a dataset composed of unique ID's. Consider this format of 3 rows each with 3 ID's:
zList = [[1412,2521,53522],
[52632,1342,1453],
[3413,342,25232]]
I am attempting to replace each ID with some corresponding data (First Name, Last Name, State, etc). Ideal output looks like this:
resultList = [[Bob, Smith, Ohio, Jane, Doe, Texas, John, Smith, Alaska],
[Jim, Bob, California, Jack, White, Virginia, John, Smith, Nevada],
[Hank, Black, Kentucy, Sarah, Hammy, Florida, Joe, Blow, Mississipi]]
I realize that it would be cleaner to add a new dimension to the results, since I am essentially expanding each ID into a new list. I avoided this because I assumed it would be easier to keep it flat, and I fear iterating through anything over 2 dimensions! Willing to consider all options...
The data I am using to match against is what you might expect:
matchData = [[1412, Bob, Smith, Ohio, lots of additional data],
[2521, Jane, Doe, Texas, Lots of Additional Data],
[3411], Jim, Black, New York, Lots of Additional Data],
[...etc...]]
Here is how I have been attempting this:
resultList = []
for i, valz in enumerate(zList):
for j, ele in enumerate(valz):
check = False
for k, valm in enumerate(matchData):
if ele == valm[0]:
resultList.append(valm)
check = True
break
if check == False:
print "error during rebuild"
pprint.pprint(resultList, width=400)
Now although it almost works, its missing 2 key things that I cant' figure out. My code dumps everything into one big list. I must be able to preserve the order and logical separation from the original data set. (remember, the original dataset was 3 rows of 3 ID's).
I also need to throw an error if there is no match found. You can see my attempt in the code above, but it does not work properly. I have tried adding this after my first if statement:
elif all(ele not in valm[15):
check = False
But I get this error: "TypeError: argument of type 'int' is not iterable"