I've a list of data and want to get values after a matching keyword from another list and append it to a dictionary. it should run until the next keyword is matched and added to a new dictionary.
a = ['Experience',
'Software Engineer',
'EY',
'Sep 2018 - Present',
'Education',
'xyz College',
'Bachelor of Technology - BTech, Computer Science',
'2014 - 2017',
'Licenses',
'The Joy of Computing using Python ',
'NPTEL',
'Issued Jan 2020']
b = ['Experience', 'Education', 'Licenses']
Expected result:
c =
{'Experience':['Software Engineer','EY', 'Sep 2018 - Present']},
{'Education':['xyz College','Bachelor of Technology - BTech, Computer Science','2014 - 2017']},
{'Licenses':
['The Joy of Computing using Python ','NPTEL','Issued Jan 2020']}
for i in a:
j = a.index(i)
k = 1
while i in b:
c[i].append(a[j 1])
if i == a[k]:
k =1
break
else:
j =1
but the logic is not correct and I'm stuck here.
CodePudding user response:
I would do a dict-comprehension and append the last element separately:
c = {keyword:a[a.index(keyword) 1:a.index(next_keyword )] for keyword, next_keyword in zip(b, b[1:])}
c[b[-1]] = a[a.index(b[-1]):]
print(c)
# {'Experience': ['Software Engineer', 'EY', 'Sep 2018 - Present']}
# {'Education': ['xyz College', 'Bachelor of Technology - BTech, Computer Science', '2014 - 2017']}
# {'Licenses': ['Licenses', 'The Joy of Computing using Python ', 'NPTEL', 'Issued Jan 2020']}
CodePudding user response:
You can loop through a, change the variable curr_key
to the key if it is found in list b
and append the elements to that.
c = {}
curr_key = None
for string in a:
if string in b:
curr_key = string
else:
c.setdefault(curr_key, []).append(string)
print(c)
Output:
{'Experience': ['Software Engineer', 'EY', 'Sep 2018 - Present'],
'Education': ['xyz College',
'Bachelor of Technology - BTech, Computer Science',
'2014 - 2017'],
'Licenses': ['The Joy of Computing using Python ',
'NPTEL',
'Issued Jan 2020']}
CodePudding user response:
I modified the variable names for better understanding:
for keyword in b:
c[keyword] = []
for i in range(len(a)):
if a[i] == keyword:
c[keyword].append(a[i 1])
c[keyword].append(a[i 2])
c[keyword].append(a[i 3])
CodePudding user response:
One way using a loop (assuming you have unique keys):
B = set(b)
c = {}
key = None
for s in a:
if s in B:
key = s
c[key] = []
elif key is not None:
c[key].append(s)
print(c)
Output:
{'Experience': ['Software Engineer', 'EY', 'Sep 2018 - Present'],
'Education': ['xyz College',
'Bachelor of Technology - BTech, Computer Science',
'2014 - 2017'],
'Licenses': ['The Joy of Computing using Python ',
'NPTEL',
'Issued Jan 2020']}
If you really want a list of individual dictionaries:
B = set(b)
c = []
key = None
for s in a:
if s in B:
key = s
d = {key: []}
c.append(d)
elif key is not None:
d[key].append(s)
print(c)
Output:
[{'Experience': ['Software Engineer', 'EY', 'Sep 2018 - Present']},
{'Education': ['xyz College', 'Bachelor of Technology - BTech, Computer Science', '2014 - 2017']},
{'Licenses': ['The Joy of Computing using Python ', 'NPTEL', 'Issued Jan 2020']}]