LeetCode: Quickly setup the Python List
作者:XD / 发表: 2021年2月23日 07:05 / 更新: 2021年2月23日 07:06 / 编程笔记 / 阅读量:2192
Recently, I have done lots of LeetCode problems. Here are some quick shortcuts to change the list.
# initial one-dimensional array with length N
Array = [0] * N
# initial two-dimensional array with M*N
Matrix = [[0] * N for _ in range(M)]
# reverse the list [0, 1, 2]->[2, 1, 0]
new_list = old_list[::-1]
new_list = old_list.reverse()
# pop out the element in the list [0, 1, 2]->[0, 2]
new_list = old_list.pop(1)
# insert the element in the list [0, 1, 2]->[0, "s", 1, 2]
new_list = old_list.insert(1, "s")
# For a four-direction traversal, create an array [-1, 0, 1, 0, -1], each of which is one of the four directions.