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

前端 未结 18 1869
-上瘾入骨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'.

    0 讨论(0)
  • 2020-11-22 03:02
    def find_dog(new_ls):
        splt = new_ls.split()
        if 'dog' in splt:
            print("True")
        else:
            print('False')
    
    
    find_dog("Is there a dog here?")
    
    0 讨论(0)
  • 2020-11-22 03:03
    x = 'aaa'
    L = ['aaa-12', 'bbbaaa', 'cccaa']
    res = [y for y in L if x in y]
    
    0 讨论(0)
  • 2020-11-22 03:04

    I am new to Python. I got the code below working and made it easy to understand:

    my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
    for str in my_list:
        if 'abc' in str:
           print(str)
    
    0 讨论(0)
  • 2020-11-22 03:06

    Question : Give the informations of abc

        a = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
    
    
        aa = [ string for string in a if  "abc" in string]
        print(aa)
    
    Output =>  ['abc-123', 'abc-456']
    
    0 讨论(0)
  • 2020-11-22 03:07

    I needed the list indices that correspond to a match as follows:

    lst=['abc-123', 'def-456', 'ghi-789', 'abc-456']
    
    [n for n, x in enumerate(lst) if 'abc' in x]
    

    output

    [0, 3]
    
    0 讨论(0)
提交回复
热议问题