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

前端 未结 18 2067
-上瘾入骨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:13

    If you want to get list of data for multiple substrings

    you can change it this way

    some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
    # select element where "abc" or "ghi" is included
    find_1 = "abc"
    find_2 = "ghi"
    result = [element for element in some_list if find_1 in element or find_2 in element] 
    # Output ['abc-123', 'ghi-789', 'abc-456']
    

提交回复
热议问题