[英]Swap first 2 rows of a 2d Python list without Numpy?
How can I swap the first 2 rows of a 2D Python list without using Numpy?如何在不使用 Numpy 的情况下交换 2D Python 列表的前 2 行? For example, if my list is:
例如,如果我的列表是:
lst = [[0,3,2],
[4,3,2],
[3,5,2]]
I want to swap [0,3,2]
with [4,3,2]
.我想用
[4,3,2]
交换[0,3,2]
] 。
Thanks!谢谢!
Something like this?像这样的东西? Or do you need it to be a function to swap when needed?
或者您是否需要它成为 function 以便在需要时进行交换?
lst = [[0,3,2],
[4,3,2],
[3,5,2]]
lst[0], lst[1] = lst[1], lst[0]
print(lst)
# [[4, 3, 2],
# [0, 3, 2],
# [3, 5, 2]
store the second value (lst[1]) in a temporary variable swap the variable将第二个值 (lst[1]) 存储在临时变量中交换变量
lst = [[0,3,2],
[4,3,2],
[3,5,2]]
a= lst[1]
lst[1] = lst[0]
lst[0] = a
hope it helps:)希望能帮助到你:)
you can simply use list functions to swap the lists.您可以简单地使用列表函数来交换列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.