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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44517191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:05:55  来源:igfitidea点击:

How to find the python list item that start with

python

提问by SPy

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_%'

回答by temasso

You have several options, but most obvious are:

您有多种选择,但最明显的是:

Using list comprehensionwith 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)

回答by RyanU

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”是您的项目列表。

回答by Senthilkumar C

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.

列表过滤将再次为您提供列表,然后需要根据您的要求进行处理。