python - 在列表中提到某些关键字后对字符串进行切片

标签 python string list

我是 python 的新手,但遇到了一个问题。我正在尝试做的是我有一个包含两个人之间对话的字符串:

str = "  dylankid: *random words* senpai: *random words* dylankid: *random words* senpai: *random words*"

我想使用 dylankid 和 senpai 作为名称从字符串创建 2 个列表:

dylankid = [ ]
senpai = [ ]

这就是我挣扎的地方,在 dylankid 列表中,我想将字符串中“dylankid”之后但在下一个“dylankid”或“senpai”之前的所有单词放在一起 senpai 列表也是如此 所以它看起来像这样

dylankid = ["random words", "random words", "random words"]
senpai = ["random words", "random words", "random words"]    

dylankid 包含来自 dylankid 的所有消息,反之亦然。

我研究过切片并使用 split()re.compile(),但我想不出一种方法来指定开始切片以及在哪里停止。

希望它足够清楚,任何帮助将不胜感激:)

最佳答案

以下代码将创建一个字典,其中键是人,值是消息列表:

from collections import defaultdict
import re

PATTERN = '''
    \s*                         # Any amount of space
    (dylankid|senpai)           # Capture person
    :\s                         # Colon and single space
    (.*?)                       # Capture everything, non-greedy
    (?=\sdylankid:|\ssenpai:|$) # Until we find following person or end of string
'''
s = "  dylankid: *random words* senpai: *random words* dylankid: *random words* senpai: *random words*"
res = defaultdict(list)
for person, message in re.findall(PATTERN, s, re.VERBOSE):
    res[person].append(message)

print res['dylankid']
print res['senpai']

它将产生以下输出:

['*random words*', '*random words*']
['*random words*', '*random words*']

关于python - 在列表中提到某些关键字后对字符串进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36530480/

相关文章:

python - 如何在 TemplateView 类旁边接收 GET/POST 值

python - 无法将 python 输出通过管道传递给程序

c - c中char数组中的空格

c# - FindAll 搜索问题

python - 如何使用 virtualenv 在 OpenShift DIY 上设置 Django 应用程序

database - 如何在 PL/SQL 中将字符串转换为数字

.net - 在 .NET(或 PowerShell)中将 Base64 字符串转换为字节数组

list - F#中的列表操作

c# - 枚举期间的 LINQ 转换

python - 如何为自动生成的 OneToOneField 关系设置默认值?