简体   繁体   English

如何找到以开头的python列表项

[英]How to find the python list item that start with

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 find the list item that start with "GFS01_"如何找到以"GFS01_"开头的列表项

In SQL I use query: select item from list where item like 'GFS01_%'在 SQL 中,我使用查询: select item from list where item like 'GFS01_%'

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 )使用filter (返回迭代器

result = filter(lambda x: x.startswith('GFS01_'), some_list)

You should try something like this :你应该尝试这样的事情:

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

where "my_list" is your list of items.其中“my_list”是您的项目列表。

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:如果你真的想要这样的字符串输出“GFS01_06-13-2017 05-10-18-38.csv”,“GFS01_xxx-xx-xx.csv”,你可以试试这个:

', '.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.列表过滤将再次为您提供列表,然后需要根据您的要求进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM