<p>对于这样的任务,你可以使用字典:</p>
<pre><code>>>> li=[['boy','121','is a male child'],['boy','121','is male'],['boy','121','is a child'],['girl','122','is a female child'],['girl','122','is a child']]
>>>
>>> d={}
>>>
>>> for i,j,k in li:
... d.setdefault((i,j),[]).append(k)
...
>>> d
{('boy', '121'): ['is a male child', 'is male', 'is a child'], ('girl', '122'): ['is a female child', 'is a child']}
</code></pre>
<blockquote>
<p><a href="https://docs.python.org/2/library/stdtypes.html#dict.setdefault" rel="nofollow"><strong>setdefault(key[, default])</strong></a></p>
<p>If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.</p>
</blockquote>
<p>如果您想让元素在一个容器中,您可以在项目上循环,并将值转换为<code>tuple</code>,然后用key:</p>
<pre><code>>>> [i+tuple(j) for i,j in d.items()]
[('boy', '121', 'is a male child', 'is male', 'is a child'), ('girl', '122', 'is a female child', 'is a child')]
</code></pre>
<p>正如@jornsharpe所说,作为一种更优雅的方式,您还可以使用<code>collections.defaultdict</code>:</p>
<pre><code>>>> from collections import defaultdict
>>>
>>> d=defaultdict(list)
>>> for i,j,k in li:
... d[i,j].append(k)
...
>>> d
defaultdict(<type 'list'>, {('boy', '121'): ['is a male child', 'is male', 'is a child'], ('girl', '122'): ['is a female child', 'is a child']})
</code></pre>