<p>如果我没看错你的意思,你会想要:</p>
<p>结果为[2,3],其中:</p>
<p><code>('abc', 'bac') = [1, 1, 0]</code>#累计值变为最终值“2”。你知道吗</p>
<p>以及</p>
<p><code>('cab', 'def') = [1, 1, 1]</code>#累计值变为最终值“3”。你知道吗</p>
<p>请参阅我的答案和下面的内联评论:</p>
<pre class="lang-py prettyprint-override"><code>dataset = [('abc', 'bac'), ('cab', 'def')]
end_result = []
for item in dataset: # use of word dataset prevents confusion related
# to lists (regardless of the use "_".)
print (item)
x = (list(zip(item[0], item[1])))
local_result = []
print (x)
for i,j in x:
if i == j:
local_result.append(0) # adding non value to equal "letters" in item.
else:
local_result.append(1) # if not equal count "1".
print (local_result)
count_results = 0
for value in local_result: # check the outcome of the local result and summarize its
# findings in an end_result list for each
# item in the dataset.
if value == 1:
count_results += 1
end_result.append(count_results)
print ("End result: %s" % end_result)
print (end_result) # prints the overal end result "2, 3"
</code></pre>
<p>输出:</p>
<blockquote>
<pre><code>[('a', 'b'), ('b', 'a'), ('c', 'c')]
[1, 1, 0]
End result: [2]
('cab', 'def')
[('c', 'd'), ('a', 'e'), ('b', 'f')]
[1, 1, 1]
End result: [2, 3]
[2, 3]```
</code></pre>
</blockquote>