<p>如果这是一个一次性的任务列表,那么理解可能是最简单的解决方案:</p>
<pre><code>>>> new_list = []
>>> new_list.append([i for i in L if i[1] == 'CFS']) # where L is your original list
>>> new_list.append([i for i in L if i[1] == 'LOOSEFREIGHT' and i[2] == 'LCL'])
>>> new_list.append([i for i in L if i[1] == 'LOOSEFREIGHT' and i[2] == 'MIXEDLCL'])
>>> from pprint import pprint as pp
>>> pp(new_list)
[[('G1', 'CFS', 'FCL', 'R1'),
('G4', 'CFS', 'FCL', 'R10'),
('G1', 'CFS', 'FCL', 'R2')],
[('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5')],
[('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')]]
</code></pre>
<p>如果您需要一个更一般的例子,您不必预先知道可能的组的数量,您可以使用<a href="http://docs.python.org/library/itertools.html#itertools.groupby" rel="nofollow">itertools.groupby</a>这样的方法:</p>
<pre><code>import itertools as it
import operator as op
new_list = []
for k,g in it.groupby(sorted(L, key=op.itemgetter(1,2)), key=op.itemgetter(1,2)):
new_list.append(list(g))
pp(new_list)
</code></pre>
<p>结果:</p>
<pre><code>[[('G1', 'CFS', 'FCL', 'R1'),
('G4', 'CFS', 'FCL', 'R10'),
('G1', 'CFS', 'FCL', 'R2')],
[('G2', 'LOOSEFREIGHT', 'LCL', 'R4'), ('G2', 'LOOSEFREIGHT', 'LCL', 'R5')],
[('G3', 'LOOSEFREIGHT', 'MIXEDLCL', 'R9')]]
</code></pre>