I have two sets of lists with similar data which I need to combine into a dictionary. The list must contain both sets of information filling in wherever the other has a "-". So far this is what I have..
aList = [['Last Name', 'First Name', '2000', '2012'],
['Roberts', 'Gloria', '-', '123000'],
['Arreguin', 'Jeffrey', '81000', '-'],
['Myers', 'George', '-', '86000'],
['Willis', 'Mabel', '112000', '-'],
['Oneal', 'Kevin', '96000', '77000'],
['Paz', 'Barbara', '-', '77000'],
['Franklin', 'Claude', '94000', '-'],
['Bradley', 'Shannon', '89000', '-'],
['Fix', 'Anna', '76000', '126000'],
['Meyer', 'Loretta', '-', '116000'],
['Daniels', 'Christina', '85000', '-'],
['Graham', 'Veronica', '-', '136000']]
newList = [['Last Name', 'First Name', '2000', '2012'],
['Meyer', 'Loretta', '123000', '-'],
['Arreguin', 'Jeffrey', '81000', '-'],
['Mielke', 'George', '137000', '-'],
['Thomas', 'Lewis', '132000', '-'],
['Harper', 'Crystal', '80000', '-'],
['Young', 'Gary', '-', '94000'],
['Franklin', 'Claude', '94000', '-'],
['Hedrick', 'James', '-', '105000'],
['Bradley', 'Shannon', '89000', '-'],
['Thigpen', 'Michael', '79000', '-'],
['Willis', 'Mabel', '112000', '-'],
['Hullinger', 'Molly', '70000', '-'],
['Myers', 'George', '-', '86000'],
['Paz', 'Barbara', '-', '77000'],
['Edwards', 'Kathryn', '117000', '97000'],
['Roberts', 'Gloria', '-', '123000'],
['Daniels', 'Christina', '-', '137000'],
['Graham', 'Veronica', '-', '136000']]
def mergeData(newList,aList):
aDict={}
for item in range(1,len(newList)):
key=(newList[item][0],newList[item][1])
value=(newList[item][2],newList[item][3])
aDict[key]=value
for item in range(1,len(aList)):
#stuck here not sure if im going right way with this
print(aDict)
Here's a sample
newList= ['Meyer', 'Loretta', '123000', '-']
aList=['Meyer', 'Loretta', '-', '116000']
so the combined dictionary should be
{(‘Meyer’, ‘Loretta’): (‘123000’,’116000’)}
for this entry.
Just loop over the first list to build a dictionary with a dict comprehension:
aDict = {(e[0], e[1]): (e[2], e[3]) for e in aList[1:]}
Next loop over the other, with a helper function to pick one or the other based on the -
values:
def pick(vals):
return vals[1] if vals[0] == '-' else vals[0]
for e in newList:
key = (e[0], e[1])
existing = aDict.get(key, ('-', '-'))
value = (e[2], e[3])
aDict[key] = tuple(map(pick, zip(existing, value)))
For your first input that results in:
{('Arreguin', 'Jeffrey'): ('81000', '-'),
('Bradley', 'Shannon'): ('89000', '-'),
('Daniels', 'Christina'): ('85000', '137000'),
('Edwards', 'Kathryn'): ('117000', '97000'),
('Fix', 'Anna'): ('76000', '126000'),
('Franklin', 'Claude'): ('94000', '-'),
('Graham', 'Veronica'): ('-', '136000'),
('Harper', 'Crystal'): ('80000', '-'),
('Hedrick', 'James'): ('-', '105000'),
('Hullinger', 'Molly'): ('70000', '-'),
('Last Name', 'First Name'): ('2000', '2012'),
('Meyer', 'Loretta'): ('123000', '116000'),
('Mielke', 'George'): ('137000', '-'),
('Myers', 'George'): ('-', '86000'),
('Oneal', 'Kevin'): ('96000', '77000'),
('Paz', 'Barbara'): ('-', '77000'),
('Roberts', 'Gloria'): ('-', '123000'),
('Thigpen', 'Michael'): ('79000', '-'),
('Thomas', 'Lewis'): ('132000', '-'),
('Willis', 'Mabel'): ('112000', '-'),
('Young', 'Gary'): ('-', '94000')}
All put together again in your merge function:
def pick(vals):
return vals[1] if vals[0] == '-' else vals[0]
def mergeData(newList, aList):
aDict = {(e[0], e[1]): (e[2], e[3]) for e in aList[1:]}
for e in newList:
key = (e[0], e[1])
existing = aDict.get(key, ('-', '-'))
value = (e[2], e[3])
aDict[key] = tuple(map(pick, zip(existing, value)))
return aDict
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.