<p>使用简单的For循环:</p>
<pre><code>list1 = ['a','b','a','b']
l2 =['e','d']
new_list = []
for char in list1:
for char_2 in l2:
item = char + '_' + char_2
new_list.append(item)
print(new_list)
</code></pre>
<p><strong>输出</strong></p>
<pre><code>['a_e', 'a_d', 'b_e', 'b_d', 'a_e', 'a_d', 'b_e', 'b_d']
</code></pre>
<hr/>
<p><strong>如果您不想重复第一个列表中的项目(这可能是预期列表中的项目)</strong></p>
<p>使用集合将确保没有多个项目</p>
<pre><code>list1 = ['a','b','a','b']
l2 =['e','d']
new_list = []
for char in set(list1):
for char_2 in l2:
item = char + '_' + char_2
new_list.append(item)
print(new_list)
['a_e', 'a_d', 'b_e', 'b_d']
</code></pre>
<h2>使用列表理解</h2>
<pre><code>list1 = ['a','b','a','b']
l2 =['e','d']
new_list = [char + '_' + char_2 for char_2 in l2 for char in set(list1)]
print(new_list)
</code></pre>
<h2>注</h2>
<p>如果对于预期的输出,您<strong>也关心元素的顺序,您可以简单地通过将集合中的第一个列表转换回列表来解决此问题,如下所示:</p>
<pre><code>list1 = ['a','b','a','b']
l2 =['e','d']
list1 = set(list1)
list1 = list(list1)
new_list = [char + '_' + char_2 for char_2 in l2 for char in list1]
print(new_list)
</code></pre>
<p><strong>输出</strong></p>
<pre><code>['a_e', 'b_e', 'a_d', 'b_d']
</code></pre>
<h2>重复问题</h2>
<ul>
<li><a href="https://stackoverflow.com/questions/65839885/how-to-merge-one-list-elements-with-another-list-elements-in-python">How to merge one list elements with another list elements in python?</a></li>
</ul>