我正在寻找一种将python列表轻松分成两半的方法。
这样,如果我有一个数组:
A = [0,1,2,3,4,5]
我将能够得到:
B = [0,1,2] C = [3,4,5]
A = [1,2,3,4,5,6] B = A[:len(A)//2] C = A[len(A)//2:]
如果需要功能:
def split_list(a_list): half = len(a_list)//2 return a_list[:half], a_list[half:] A = [1,2,3,4,5,6] B, C = split_list(A)