Home > database >  Delete matching items of list
Delete matching items of list

Time:10-30

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'] ]

CodePudding user response:

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']]

CodePudding user response:

Not sure if I got the question well, but oh well, here we go:

Suppose we have elements of a list that look like 'img/Sleeveless_Vented_Blouse/img_00000037.jpg'. Provided that we need to remove all the elements of the list that contain 'Sleeveless', we can do it like this:

to_be_removed = 'Sleeveless'
for element in elements_list:
    if to_be_removed in element:
        elements_list.remove(element)

Simply because:

>>> a = 'img/Sleeveless_Vented_Blouse/img_00000037.jpg'
>>> 'Sleeveless' in a
True

Hope I answered the right question.

  • Related