Home > Software design >  How to shorten elements in a list that contain specific category names and return the category?
How to shorten elements in a list that contain specific category names and return the category?

Time:05-04

I am very new to python and coding so need some ideas on how to tackle this.

I have a list on elements which look like this -

lst = ['res/entertainment/578.txt',
 'res/tv/232.txt',
 'res/news/323.txt',
 'res/kids/323.txt',
 'res/entertainment/323.txt',
 'res/kids/325.txt',
 'res/kids/542.txt',
 'res/kids/008.txt']

categories = ['entertainment', 'tv', 'news', 'kids']

I want to create a sublist of mylist which only contains the relevant category related to the element in the list.

new_list = ['entertainment',
'tv',
'news',
'kids',
'entertainment',
'kids',
'kids',
'kids]

Any help will be much appreciated.

CodePudding user response:

No need for a regex, use a simple list comprehension with split:

lst = ['res/entertainment/578.txt',
 'res/tv/232.txt',
 'res/news/323.txt',
 'res/kids/323.txt',
 'res/entertainment/323.txt',
 'res/kids/325.txt',
 'res/kids/542.txt',
 'res/kids/008.txt']

new_list = [x.split('/')[1] for x in lst]

and if you need to filter based on categories (python ≥ 3.8):

categories = ['entertainment', 'tv', 'news', 'kids']
S = set(categories)
new_list = [c for x in lst if (c:=x.split('/')[1]) in S]

output:

['entertainment',
 'tv',
 'news',
 'kids',
 'entertainment',
 'kids',
 'kids',
 'kids']
  • Related