python - 在python中过滤列表

标签 python regex

我有一个列表列表,我想在 python 中的指定字符串之后/之前/之间分隔元素。

例子:

在指定的字符串之后:

input1 : [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]

output1 : ddddd, eeeeee, ffffff

在指定字符串之间:

input2 : [['aaaaa', 'bbbbb', 'cccc', 'specified string1', 'ddddd', 'eeeeee', 'ffffff', 'specified string2', 'qqqq', 'wwww', 'sssss']]

output2 : ddddd, eeeeee, ffffff

在指定字符串之前:

input3 : [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]

output3 : aaaaa, bbbbb, cccc

最佳答案

您可以通过简单地访问列表中的列表:

lst = list_of_list[0]

现在您想搜索“指定的字符串”。您可以:

lst.index('specified string') 

这将返回字符串出现的索引。最后,您可以使用切片来获取您想要的子列表。

尝试这样做,如果您遇到问题,请在此处发布,我们会帮助您。

关于python - 在python中过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27211756/

相关文章:

python - 我无法制作 1x1 矩形

c# - 使用正则表达式平衡组来匹配嵌套标签

JavaScript 正则表达式 : how to single out english words AND non-english characters AND special characters?

python - 更新 Tkinter 列表框而不退出并重新进入?

python - Django 中间件的正确顺序是什么?

java - 正则表达式:如果我想在给定模式中找到 "\"和 "s",我需要做什么?

python - 如何在python中的sql查询中使用MySQL模式匹配和绑定(bind)参数?

python 正则表达式将列表条目的名称拆分为列表名称和索引

python - 为什么 Popen ('ping URL' ).communicate() 在 Windows 中有效而在 Ubuntu 中无效?

python - 如何在 Google App Engine 上运行用 Python 编写的 MQTT 订阅者脚本