Check if a Python list item contains a string inside another string

前端 未结 18 2200
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:18

I have a list:

my_list = [\'abc-123\', \'def-456\', \'ghi-789\', \'abc-456\']

and want to search for items that contain the string \'

18条回答
  •  天命终不由人
    2020-11-22 03:02

    I did a search, which requires you to input a certain value, then it will look for a value from the list which contains your input:

    my_list = ['abc-123',
            'def-456',
            'ghi-789',
            'abc-456'
            ]
    
    imp = raw_input('Search item: ')
    
    for items in my_list:
        val = items
        if any(imp in val for items in my_list):
            print(items)
    

    Try searching for 'abc'.

提交回复
热议问题