我是 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/