I have 2 lists:
my_values = ['0,78', '0,40', '0,67']
my_list = [
['Morocco', 'Meat', '190,00', '0,15'],
['Morocco', 'Meat', '189,90', '0,32'],
['Morocco', 'Meat', '189,38', '0,44'],
['Morocco', 'Meat', '188,94', '0,60'],
['Morocco', 'Meat', '188,49', '0,78'],
['Morocco', 'Meat', '187,99', '0,101'],
['Spain', 'Meat', '190,76', '0,10'],
['Spain', 'Meat', '190,16', '0,20'],
['Spain', 'Meat', '189,56', '0,35'],
['Spain', 'Meat', '189,01', '0,40'],
['Spain', 'Meat', '188,13', '0,75'],
['Spain', 'Meat', '187,95', '0,78'],
['Italy', 'Meat', '190,20', '0,11'],
['Italy', 'Meat', '190,10', '0,31'],
['Italy', 'Meat', '189,32', '0,45'],
['Italy', 'Meat', '188,61', '0,67'],
['Italy', 'Meat', '188,01', '0,72'],
['Italy', 'Meat', '187,36', '0,80'],
]
I have a code which is checking the following:
- For Morocco it checks at what
index[2]
inmy_list
isindex[3]
inmy_values == 0,78
- For Spain it checks at what
index[2]
inmy_list
isindex[3]
inmy_values == 0,40
- For Italy it checks at what
index[2]
in my_list
isindex[3]
inmy_values == 0,67
Now I have a problem, as you can see 0,78
in my_values
is present in Morocco AND Spain, I only want it to check it for Morocco.
This is my code:
yet_another_list = [i[2] for i in my_list if i[3] in my_values]
print(yet_another_list)
This is my output:
['188,49', '189,01', '187,95', '188,61']
This is my prefered output:
['188,49', '189,01', '188,61']
As you can see I want index[1]
of my_values
to be only used for Morocco
, and index[2]
for Spain
etc... Please note that in my officla dataset my_lists
contains a lot more countries...
#ADDED. I even tried Pandas but still received the same output.
df=pd.DataFrame(my_list)
df['Filter']=np.where([i in my_values for i in df[3]],"Yes","")
my_out_list=list(df[2][df['Filter']=='Yes'])
print(my_out_list)
>>
['188,49', '189,01', '187,95', '188,61']