如何添加两个列表,以使生成的列表保持其他列表不变:
['5','6','7'] + ['1'] + ['9','7'] = [['5','6','7'], ['1'], ['9','7']]
在python中可以这样做吗?你知道吗
当前代码:
def appendy(list_o_list):
temp_l = []
for l in list_o_list:
temp_l.append(list(l))
new_list=[]
new_list = [a + b for a, b in itertools.combinations(temp_l, 2)]
print("app",new_list)
return (new_list)
appendy([('g3', 'g1'), ('g3', 'g2')])
+
意味着对象的串联,所以直观地说:正如Darkchili Slayer所提到的,您可以通过附加将列表嵌入到另一个列表中。事实上,一个相当简单的解决方案就是:
如果你想变得有趣,你可以使用特殊的variable argument operator, *:
你甚至可以这样做,让它更容易阅读:
最后,值得注意的是,列表有一个
extend
函数,它将每个项附加到另一个列表中。您可以使用它来简化第一个示例:在本例中,
extend
函数不是很有用,因为输入和输出完全相同。你知道吗它不是添加列表,而是附加列表。只需使用.append()就很容易了
只要做:
所有原始列表将保持不变,
resulting_list
将包含联接的列表。你想做什么并不完全清楚。你知道吗相关问题 更多 >
编程相关推荐