擅长:python、mysql、java
<p>因此,为了做到这一点,除非您想进行自然语言处理,否则您需要指定对什么构成名称和什么构成动作的限制。例如,如果您说名称是以大写字母开头的拉丁字母字符的集合,而非名称的任何操作(例如,它不能以大写字母开头)可以执行以下操作:</p>
<pre><code>import re
p = re.compile(r'^(.*)([A-Z][a-z]+)(.*)$')
a = 'Bob plays, draws Jane, Nicky lives in a house'
# split expressions by comma
b = a.split(',')
# create an empty dictionary to hold the result
d = {}
# process all comma-separated expressions
for i in b:
# strip removes blank lines before and after the string
i = i.strip()
res = re.match(p, i)
if res.group(1) != '' and res.group(3) != '':
raise ValueError("Entry ({}) cannot be processed".format(i))
person = res.group(2)
action = res.group(1) if res.group(1) != '' else res.group(3)
# set the dictionary entry with key - first element
# and value - second
d[person.strip()] = action.strip()
print(d)
</code></pre>
<p>在这里,我只是使用正则表达式来指定名称</p>