How to find the python list item that start with

后端 未结 3 557
庸人自扰
庸人自扰 2020-12-14 12:32

I have the list that contains some items like:

\"GFS01_06-13-2017 05-10-18-38.csv\"
\"Metadata_GFS01_06-13-2017 05-10-18-38.csv\"

How to fi

相关标签:
3条回答
  • 2020-12-14 12:59

    You have several options, but most obvious are:

    Using list comprehension with a condition:

    result = [i for i in some_list if i.startswith('GFS01_')]
    

    Using filter (which returns iterator)

    result = filter(lambda x: x.startswith('GFS01_'), some_list)
    
    0 讨论(0)
  • 2020-12-14 13:26

    You should try something like this :

    [item for item in my_list if item.startswith('GFS01_')]
    

    where "my_list" is your list of items.

    0 讨论(0)
  • 2020-12-14 13:26

    If you really want the string output like this "GFS01_06-13-2017 05-10-18-38.csv","GFS01_xxx-xx-xx.csv", you could try this:

    ', '.join([item for item in myList if item.startswith('GFS01_')])
    

    Or with quotes

    ', '.join(['"%s"' % item for item in myList if item.startswith('GFS01_')])
    

    Filtering of list will gives you list again and that needs to be handled as per you requirement then.

    0 讨论(0)
提交回复
热议问题