<p>将您的状况从:</p>
<pre><code> if c[z][y] == x:
pdb.set_trace()
temp.append(c[z][y])
answers.append(y+1)
</code></pre>
<p>致:</p>
<pre><code> if c[z][y] == x:
pdb.set_trace()
if x not in temp:
temp.append(c[z][y])
answers.append(y+1)
</code></pre>
<p>仅当临时列表尚未出现时才添加</p>
<p>运行示例:</p>
<pre><code>_list = ['Adarsh Kumar Goel', 'Pilibhit Tiger Reserve', 'Sheikha Bodour Bint Sultan Al Qasimi', 'Saudi Central Bank', 'Vijay Kumar Sinha', 'Odisha', 'Niger', 'Qatar', '6000 cr', 'Greg Barclay', 'Tripura', 'Geneva', 'Thaawarchand Gehlot', 'V.K. Saraswat', 'SBI']
c = [('Adarsh Kumar Goel', 'Swatanter Kumar', 'Lokeshwar Singh Panta', 'K. Ramakrishnan', 'Saibal Dasgupta'), ('Panna Tiger Reserve', 'Rajaji Tiger reserve', 'Pilibhit Tiger Reserve', 'Manas Tiger Reserve', 'Corbett Tiger Reserve'), ('Haifaa Al Mansour', 'Nour El Sherbini', 'Sheikha Bodour Bint Sultan Al Qasimi', 'Sheikha Aisha bint Rashid Al Khalifa', 'Sheikha Mozah Al Maktoum'), ('Bank of Saudi', 'Saudi Central Bank', 'Central Bank of Saudi', 'Reserve Bank of Saudi Arab', 'Federal Bank of Saudi'), ('Sushil Modi', 'Vijay Kumar Sinha', 'Renu Devi', 'Tejashwi Yadav', 'Tar Kishor Prasad'), ('Maharashtra', 'Kerala', 'Madhya Pradesh', 'Odisha', 'Tripura'), ('Kenya', 'Brazil', 'Ethiopia', 'Niger', 'Rwanda'), ('Bahrain', 'Qatar', 'Jordan', 'UAE', 'Saudi Arabia'), ('2000 cr', '4000 cr', '6000 cr', '7500 cr', '10,000 cr'), ('Shashank Manohar', 'Greg Barclay', 'Imran Khwaja', 'Anil Kumble', 'Manu Sawhney'), ('Odisha', 'Madhya Pradesh', 'Tripura', 'Goa', 'Uttar Pradesh'), ('Zurich', 'Istanbul', 'Geneva', 'New York', 'Kabul'), ('Narendra Modi', 'Thaawarchand Gehlot', 'Ravi Shankar Prasad', 'Narendra Singh Tomar', 'Smriti Irani'), ('Jyoti Sinha', 'V.K. Saraswat', 'Vinod Kumar Yadav', 'Amitabh Kant', 'Rajiv Kumar'), ('Axis Bank', 'PNB', 'SBI', 'HDFC', 'RBL Bank')]
temp = []
answers = []
for x in _list:
for z in range(len(c)):
for y in range(len(c[z])):
if c[z][y] == x:
if x not in temp:
temp.append(c[z][y])
answers.append(y+1)
assert temp == _list
assert answers == [1, 3, 3, 2, 2, 4, 4, 2, 3, 2, 5, 3, 2, 2, 3]
print(temp)
print(answers)
</code></pre>
<p>顺便说一句:在我看来,最好不要执行<code>answers.append(y+1)</code>,而是执行<code>answers.append(y)</code>,因为计算机中的数组是从0开始的,所以可以通过不向索引中添加+1来避免一些误解。除非,否则该索引y+1仅用于向非程序员显示数据</p>
<p><strong>更好的方法:</strong></p>
<p>但是,由于在<code>c</code>列表中有一些重复的条目,因此最好使用映射来保留给定键的多个可能答案:</p>
<pre><code>temp = []
answers = {}
for x in _list:
for z in range(len(c)):
for y in range(len(c[z])):
if c[z][y] == x:
if x not in temp:
temp.append(c[z][y])
answers[x] = [y+1]
elif x in temp:
answers[x].append(y + 1)
assert temp == _list
print(_list)
print(answers)
</code></pre>
<p><strong>输出</strong>:</p>
<pre><code>{'Adarsh Kumar Goel': [1], 'Pilibhit Tiger Reserve': [3], 'Sheikha Bodour Bint Sultan Al Qasimi': [3], 'Saudi Central Bank': [2], 'Vijay Kumar Sinha': [2], 'Odisha': [4, 1], 'Niger': [4], 'Qatar': [2], '6000 cr': [3], 'Greg Barclay': [2], 'Tripura': [5, 3], 'Geneva': [3], 'Thaawarchand Gehlot': [2], 'V.K. Saraswat': [2], 'SBI': [3]}
</code></pre>