我有一个列表,如下所示:
my_list =
[['BCN', '01-01-2021', '08:30:30'],
['MCR', '01-01-2021', '12:48:12'],
['TNG', '01-01-2021', '10:01:02']]
对于my_list
中的每个列表,我试图加入index[1]
+index[2]
并将其写回新列表
这是我的代码:
new_list = []
for row in my_list:
' '.join(row[1] + row[2])
new_list.append(row)
print(new_list)
这是我的输出:
[['BCN', '01-01-2021', '08:30:30'],
['MCR', '01-01-2021', '12:48:12'],
['TNG', '01-01-2021', '10:01:02']]
这是我的预期输出:
[['BCN', '01-01-2021 08:30:30'],
['MCR', '01-01-2021 12:48:12'],
['TNG', '01-01-2021 10:01:02']]
这一行中没有赋值,也没有执行预期的操作,因为它首先附加字符串(+),然后在每个字符之间放置空格(联接):
这就是你想要做的:
您没有更改行的值
尝试:
您只是添加每行的最后两个元素,而不是将其分配到任何位置。试试这个:
较短的版本为:
编辑:
或者
更多项目:
相关问题 更多 >
编程相关推荐