How do I split the elements in this element based on the string before the dot without explicitly writing it in code?
lst = ['ds_a.cola','ds_a.colb','ds_b.cola','ds_b.colb']
Since there are two variants of 'ds'. I want two lists.
lst_dsa = ['ds_a.cola','ds_a.colb']
lst_dsb = ['ds_b.cola','ds_b.colb']
My old code was:
lst_dsa = []
lst_dsb = []
for item in lst :
if "ds_a" in item:
lst_dsa.append(item)
else:
lst_dsb.append(item)
But I can't use this since there might be more than 2, like, ds_c,ds_d.... How do I achieve this in python?
CodePudding user response:
Use a dict and hold the data
from collections import defaultdict
lst = ['ds_a.cola','ds_a.colb','ds_b.cola','ds_b.colb','ds_x.cola','ds_x.colb']
data = defaultdict(list)
for entry in lst:
a,_ = entry.split('.')
data[a].append(entry)
print(data)
output
defaultdict(<class 'list'>, {'ds_a': ['ds_a.cola', 'ds_a.colb'], 'ds_b': ['ds_b.cola', 'ds_b.colb'], 'ds_x': ['ds_x.cola', 'ds_x.colb']})
CodePudding user response:
Try itertools.groupby
:
>>> from itertools import groupby
>>> [list(v) for _, v in groupby(lst, key=lambda x: x[x.find('_') 1])]
[['ds_a.cola', 'ds_a.colb'], ['ds_b.cola', 'ds_b.colb']]
>>>
CodePudding user response:
You can map them:
from collections import defaultdict
lst = ['ds_a.cola','ds_a.colb','ds_b.cola','ds_b.colb']
ds_dict = defaultdict(list)
for item in lst:
key, value = item.split(".")
ds_dict[key].append(value)
print(dict(ds_dict))
Output:
{'ds_a': ['cola', 'colb'], 'ds_b': ['cola', 'colb']}
CodePudding user response:
try this:
d = dict()
for item in lst:
key = item.split(".")[0]
if key not in d.keys():
d[key] = list()
d[key].append(item)