Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python正则表达式按person对句子进行分组';姓名_Python 3.x_Regex - Fatal编程技术网

Python 3.x Python正则表达式按person对句子进行分组';姓名

Python 3.x Python正则表达式按person对句子进行分组';姓名,python-3.x,regex,Python 3.x,Regex,我有以下案文: "- Nike: Hey, where are you?\n10/6/20, 8:51 - Mike: Soon\n10/6/20, 8:55 - Nike: how are you guy?\n10/4/20, 8:55 - Mike: It's okay\n10/4/20, 9:05" 我想列出如下两个清单: nike = ["Hey, where are you?", "how are you guy?"] mi

我有以下案文:

"- Nike: Hey, where are you?\n10/6/20, 8:51 - Mike: Soon\n10/6/20, 8:55 - Nike: how are you guy?\n10/4/20, 8:55 - Mike: It's okay\n10/4/20, 9:05"
我想列出如下两个清单:

nike = ["Hey, where are you?", "how are you guy?"]
mike = ["Soon", "It's okay"]
你知道我怎么能做这样的事吗

谢谢大家

import re

s = "- Nike: Hey, where are you?\n10/6/20, 8:51 - Mike: Soon\n10/6/20, 8:55 - Nike: how are you guy?\n10/4/20, 8:55 - Mike: It's okay\n10/4/20, 9:05"

out = {}
for name, sentence in re.findall(r'([A-Za-z]+):\s*(.*)$', s, flags=re.M):
    out.setdefault(name, []).append(sentence)

print(out)
印刷品:

{'Nike': ['Hey, where are you?', 'how are you guy?'], 'Mike': ['Soon', "It's okay"]}