-4

I have a list containing some image file names as below:

print(image_list)

['10002_Mona_Lisa.jpg',
 '10004_Desperation.jpg',
 '10009_Brother_Bear.jpg',
 '10010_Brother_Bear_2.jpg',
 '10016_Ghosts_of_Mars.jpg',
 '100192_Destination_Infestation.jpg',
 '100271_A_Letter_to_Momo.jpg',
 '10028_Honey.jpg',
 '100297_2KM2_-_A_Square_View.jpg',
 '100402_Captain_America_The_Winter_Soldier.jpg',
 '100450_Trapped_in_the_Closet_Chapters_1-12.jpg',
 '10045_District_B13.jpg']

Suppose i want to find the exact name from the above list just by giving a word or set of words for example: ghosts which should give the output as below:

10016_Ghosts_of_Mars.jpg
Nishant
  • 1,063
  • 13
  • 40

1 Answers1

0
In [33]: a = ['10002_Mona_Lisa.jpg',
...:  '10004_Desperation.jpg',
...:  '10009_Brother_Bear.jpg',
...:  '10010_Brother_Bear_2.jpg',
...:  '10016_Ghosts_of_Mars.jpg',
...:  '100192_Destination_Infestation.jpg',
...:  '100271_A_Letter_to_Momo.jpg',
...:  '10028_Honey.jpg',
...:  '100297_2KM2_-_A_Square_View.jpg',
...:  '100402_Captain_America_The_Winter_Soldier.jpg',
...:  '100450_Trapped_in_the_Closet_Chapters_1-12.jpg',
...:  '10045_District_B13.jpg']

In [41]: def get(*words):
...:         return "\n".join(i for j in words for i in a if j.lower() in i.lower())

In [44]: print(get("ghost", "Brother"))
   10016_Ghosts_of_Mars.jpg
   10009_Brother_Bear.jpg
   10010_Brother_Bear_2.jpg

The function takes any number of words, check for it's presence in the file name and gives out if it's present in the file name

Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
bigbounty
  • 16,526
  • 5
  • 37
  • 65