<p>您需要为小列表中的元素创建一个映射(字典):</p>
<pre><code>m = {k: v for k, v in zip(map(tuple, [tc1, tc2, tc3, tc4])), ["tc1", "tc2", "tc3", "tc4"])}
>>> {('KK02', 'FE34'): 'tc3', ('AB21', 'BG54'): 'tc2', ('CF54', 'SD62'): 'tc4', ('HG89', 'NS72'): 'tc1'}
</code></pre>
<p>然后可以使用迭代器在列表上循环:</p>
<pre class="lang-python prettyprint-override"><code>itr = iter(full_log)
for i in itr:
if i != "Error":
n = next(itr)
if n != "Error":
if (i, n) in m:
print m[(i, n)]
else:
print "Er"
else:
print "Er"
>>> tc2
tc1
Er
tc4
tc3
</code></pre>
<p>如果您不介意扩展第一个列表中的“错误”条目:</p>
<pre><code>full_log2 = [item for sublist in [[i] if i != "Error" else ["Error", "Error"] for i in full_log] for item in sublist]
>>> ['AB21', 'BG54', 'HG89', 'NS72', 'Error', 'Error', 'CF54', 'SD62', 'KK02', 'FE34']
</code></pre>
<p>然后你可以使用列表理解:</p>
<pre><code>print [m[(full_log2[i], full_log2[i+1])] if (full_log2[i], full_log2[i+1]) in m else "Er" for i in range(0, len(full_log2)-1, 2)]
>>> ['tc2', 'tc1', 'Er', 'tc4', 'tc3']
</code></pre>