I have two lists of equal size, one has the type of data (comes in succession):
types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
The second list is about the data:
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]
from data list, I know that "xx","tr"
are "vigi"
and "kl","ut","ew"
are "fruits"
and so on.
What I need in each time divide the data
into two datasets with:
data1 = data[indices for type "vigi"]
data2 = data[indices for the remaining (i.e. data for "fruits" and "nothing")]
A second time will have:
data1 = data[indices for type "fruits"]
data2 = data[indices for the remaining (i.e. data for "vigi" and "nothing")]
And so on ..
Any help with that please.
You can use zip()
function:
types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]
data1 = [d for t, d in zip(types, data) if t == 'vigi']
data2 = [d for t, d in zip(types, data) if t != 'vigi']
print(data1)
print(data2)
Prints:
['xx', 'tr']
['kl', 'ut', 'ew', 'uy', 'lp', 'eq', 'aq']
Other version (that iterates over the lists only once):
types = ["vigi","vigi","fruits","fruits","fruits","nothing","nothing","nothing","nothing"]
data = ["xx","tr","kl","ut","ew","uy","lp","eq","aq"]
data1, data2 = [], []
for t, d in zip(types, data):
if t == 'vigi':
data1.append(d)
else:
data2.append(d)
print(data1)
print(data2)