擅长:python、mysql、java
<p>您可以使用regex将单词与大写字母匹配,并使用dict comprehension创建dict:</p>
<pre><code>import re
s = 'Bob plays, draws Jane, Nicky lives in a house'
{re.search(r'\b[A-Z].*?\b', text.strip())[0]:text.replace(re.search(r'\b[A-Z].*?\b', text.strip())[0], '').strip() for text in s.split(',')}
</code></pre>
<p>输出:</p>
<pre><code>{'Bob': 'plays', 'Jane': 'draws', 'Nicky': 'lives in a house'}
</code></pre>
<p>或者对于Python 3.8或更新版本:</p>
<pre><code>{match:text.replace(match, '').strip() for text in s.split(',') if (match := re.search(r'\b[A-Z].*?\b', text.strip())[0])}
</code></pre>