Skip to content
Advertisement

Delete matching items of list

so i am trying to match the elements of out_list with the main_list second index values and if they match , i want to delete that certain label. Like in out list we have one label “sleeveless” so I want it deleted wherever it is in the 2nd index of main list. For example after eradication of matching words from out_list, the first list element for mainlist will be come:

['img/Sleeveless_Vented_Blouse/img_00000037.jpg', 'printed,chiffon,floral', '0']

This is what i have tried, i don’t know how to go further

main_list=[ ['img/Sleeveless_Vented_Blouse/img_00000037.jpg', 'printed,sleeveless,chiffon,floral', '0'], ['img/Sleeveless_Vented_Blouse/img_00000038.jpg', 'printed,ruffle', '0'], ['img/Sleeveless_Vented_Blouse/img_00000040.jpg', 'sleeveless', '0'] ]
    out_list = ['collar', 'floralprint', 'longsleeve', 'pink', 'pintuck', 'red', 'shirt', 'sleeve', 'sleeveless', 'split', 'trim', 'tunic', 'v-neck']
    for i in range(main_list):
      att=m[i].split(",")
      for t in out_list:
      if t in att:

Below is a test dataset:

out_list = ['collar', 'floralprint', 'longsleeve', 'pink', 'pintuck', 'red', 'shirt', 'sleeve', 'sleeveless', 'split', 'trim', 'tunic', 'v-neck']
main_list=[['img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', 'pleated,sheer', '0'], ['img/Sheer_Pleated-Front_Blouse/img_00000002.jpg', 'pleated,sheer', '0'], ['img/Sheer_Pleated-Front_Blouse/img_00000009.jpg', 'sheer', '0'], ['img/Sheer_Pleated-Front_Blouse/img_00000010.jpg', 'sheer,woven', '0'], ['img/Sheer_Pleated-Front_Blouse/img_00000014.jpg', 'summer', '0'], ['img/Sleeveless_Vented_Blouse/img_00000037.jpg', 'printed,sleeveless,shirt,chiffon,floral', '0'], ['img/Sleeveless_Vented_Blouse/img_00000038.jpg', 'printed,ruffle', '0'], ['img/Sleeveless_Vented_Blouse/img_00000040.jpg', 'sleeveless', '0'] ]

Advertisement

Answer

You could use a regex built on out_list:

NB. assuming out_list = ['collar', 'floralprint', 'longsleeve', 'pink', 'pintuck', 'red', 'shirt', 'sleeve', 'sleeveless', 'split', 'trim', 'tunic', 'v-neck']

import re
regex = re.compile('(%s)' % '|'.join(sorted(out_list)[::-1]), flags=re.I)


filtered_main = [[l[0],
                  ','.join(i for i in l[1].split(',')
                           if i not in map(str.lower, regex.findall(l[0]))),
                  l[2]]
                 for l in main_list
                ]

output:

[['img/Sleeveless_Vented_Blouse/img_00000037.jpg', 'printed,chiffon,floral', '0'],
 ['img/Sleeveless_Vented_Blouse/img_00000038.jpg', 'printed,ruffle', '0'],
 ['img/Sleeveless_Vented_Blouse/img_00000040.jpg', '', '0']]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement