如何将列表的第 i 个元素附加到列表列表中第 i 个列表的第一个条目?
How to append i'th element of a list to first entry in i'th list in list of lists?
即:列表中的每个元素最终成为列表列表中相应列表中的第一个元素。
喜欢以下内容:
List_of_Lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = [1,2,3]
结果:
New_List_of_List = [[1,1,2,3][2,2,3,4],[3,4,4,4]]
我尝试了各种追加和插入方法,但主要问题是我不确定如何将 List_of_Lists 中的单个列表和列表中的元素混合在一起。
可以在0位置插入元素
lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = [1,2,3]
for i in range(len(lists)):
lists[i].insert(0,List1[i])
print(lists)
输出:
[[1, 1, 2, 3], [2, 2, 3, 4], [3, 4, 4, 4]]
另一个解决方案:
List_of_Lists = [[1, 2, 3], [2, 3, 4], [4, 4, 4]]
List1 = [1, 2, 3]
out = [[v, *subl] for v, subl in zip(List1, List_of_Lists)]
print(out)
打印:
[[1, 1, 2, 3], [2, 2, 3, 4], [3, 4, 4, 4]]
试试这个:
for idx, sub_list in enumerate(List_of_Lists):
sub_list.insert(0, List1[idx])
The above code will modify your List_of_Lists, if you don't want that please create a copy and then loop through the copy.
这是我编写的示例代码,用于在子列表中的任意位置插入一个值。
import copy
List_of_Lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = ['Z','Y','X']
def Insert_List_of_Lists(List_X,index,value):
temp_list=copy.deepcopy(List_X)
if index<=len(temp_list):
for i in range(len(temp_list)):
temp_list[i].insert(index,value)
else:
for i in range(len(temp_list)):
temp_list[i].insert(len(temp_list),value)
print("Given index Out of range, value appended")
return(temp_list)
New_List_of_List = Insert_List_of_Lists(List_of_Lists,3,List1[0])
New_List_of_List
输出:
[[1, 2, 3, 'Z'], [2, 3, 4, 'Z'], [4, 4, 4, 'Z']]
当我们尝试从索引中添加一个值时:
X_list = Insert_List_of_Lists(List_of_Lists,8,'J')
X_list
输出:
Given index Out of range, value appended
[[1, 2, 3, 'J'], [2, 3, 4, 'J'], [4, 4, 4, 'J']]
即:列表中的每个元素最终成为列表列表中相应列表中的第一个元素。
喜欢以下内容:
List_of_Lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = [1,2,3]
结果:
New_List_of_List = [[1,1,2,3][2,2,3,4],[3,4,4,4]]
我尝试了各种追加和插入方法,但主要问题是我不确定如何将 List_of_Lists 中的单个列表和列表中的元素混合在一起。
可以在0位置插入元素
lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = [1,2,3]
for i in range(len(lists)):
lists[i].insert(0,List1[i])
print(lists)
输出:
[[1, 1, 2, 3], [2, 2, 3, 4], [3, 4, 4, 4]]
另一个解决方案:
List_of_Lists = [[1, 2, 3], [2, 3, 4], [4, 4, 4]]
List1 = [1, 2, 3]
out = [[v, *subl] for v, subl in zip(List1, List_of_Lists)]
print(out)
打印:
[[1, 1, 2, 3], [2, 2, 3, 4], [3, 4, 4, 4]]
试试这个:
for idx, sub_list in enumerate(List_of_Lists):
sub_list.insert(0, List1[idx])
The above code will modify your List_of_Lists, if you don't want that please create a copy and then loop through the copy.
这是我编写的示例代码,用于在子列表中的任意位置插入一个值。
import copy
List_of_Lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = ['Z','Y','X']
def Insert_List_of_Lists(List_X,index,value):
temp_list=copy.deepcopy(List_X)
if index<=len(temp_list):
for i in range(len(temp_list)):
temp_list[i].insert(index,value)
else:
for i in range(len(temp_list)):
temp_list[i].insert(len(temp_list),value)
print("Given index Out of range, value appended")
return(temp_list)
New_List_of_List = Insert_List_of_Lists(List_of_Lists,3,List1[0])
New_List_of_List
输出:
[[1, 2, 3, 'Z'], [2, 3, 4, 'Z'], [4, 4, 4, 'Z']]
当我们尝试从索引中添加一个值时:
X_list = Insert_List_of_Lists(List_of_Lists,8,'J')
X_list
输出:
Given index Out of range, value appended
[[1, 2, 3, 'J'], [2, 3, 4, 'J'], [4, 4, 4, 'J']]