cor*_*ews 130 python split list
我正在寻找一种方法来轻松地将python列表分成两半.
所以,如果我有一个数组:
A = [0,1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
我能得到:
B = [0,1,2]
C = [3,4,5]
Run Code Online (Sandbox Code Playgroud)
Jas*_*oon 193
A = [1,2,3,4,5,6]
B = A[:len(A)//2]
C = A[len(A)//2:]
Run Code Online (Sandbox Code Playgroud)
如果你想要一个功能:
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)
Run Code Online (Sandbox Code Playgroud)
Chr*_*heD 76
一个更通用的解决方案(您可以指定所需的部件数量,而不仅仅是分成两半):
编辑:更新帖子来处理奇数列表长度
EDIT2:根据Brians的信息性评论再次更新帖子
def split_list(alist, wanted_parts=1):
length = len(alist)
return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
for i in range(wanted_parts) ]
A = [0,1,2,3,4,5,6,7,8,9]
print split_list(A, wanted_parts=1)
print split_list(A, wanted_parts=2)
print split_list(A, wanted_parts=8)
Run Code Online (Sandbox Code Playgroud)
Jar*_*aro 40
f = lambda A, n=3: [A[i:i+n] for i in range(0, len(A), n)]
f(A)
Run Code Online (Sandbox Code Playgroud)
n - 预定义的结果数组长度
Sia*_*and 30
def split(arr, size):
arrs = []
while len(arr) > size:
pice = arr[:size]
arrs.append(pice)
arr = arr[size:]
arrs.append(arr)
return arrs
Run Code Online (Sandbox Code Playgroud)
测试:
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
print(split(x, 5))
Run Code Online (Sandbox Code Playgroud)
结果:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]
Run Code Online (Sandbox Code Playgroud)
小智 11
这是一个常见的解决方案,将arr分成计数部分
def split(arr, count):
return [arr[i::count] for i in range(count)]
Run Code Online (Sandbox Code Playgroud)
dbr*_*dbr 10
使用列表切片。语法基本上是my_list[start_index:end_index]
>>> i = [0,1,2,3,4,5]
>>> i[:3] # same as i[0:3] - grabs from first to third index (0->2)
[0, 1, 2]
>>> i[3:] # same as i[3:len(i)] - grabs from fourth index to end
[3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
要获得列表的前半部分,请从第一个索引切片到len(i)//2(//整数除法的位置 - 所以3//2 will give the floored result of1 , instead of the invalid list index of1.5`):
>>> i[:len(i)//2]
[0, 1, 2]
Run Code Online (Sandbox Code Playgroud)
..并交换周围的值以获得下半部分:
>>> i[len(i)//2:]
[3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
小智 10
如果你不关心订单......
def split(list):
return list[::2], list[1::2]
Run Code Online (Sandbox Code Playgroud)
list[::2]从第0个元素开始获取列表中的每个第二个元素.
list[1::2]从第一个元素开始获取列表中的每个第二个元素.
def splitter(A):
B = A[0:len(A)//2]
C = A[len(A)//2:]
return (B,C)
Run Code Online (Sandbox Code Playgroud)
我测试了,并且需要双斜杠来强制在python 3中进行int划分.我的原始帖子是正确的,尽管wysiwyg在Opera中出现了某种原因.
如果你有一个很大的列表,最好使用itertools并编写一个函数来根据需要产生每个部分:
from itertools import islice
def make_chunks(data, SIZE):
it = iter(data)
# use `xragne` if you are in python 2.7:
for i in range(0, len(data), SIZE):
yield [k for k in islice(it, SIZE)]
Run Code Online (Sandbox Code Playgroud)
你可以这样使用:
A = [0, 1, 2, 3, 4, 5, 6]
size = len(A) // 2
for sample in make_chunks(A, size):
print(sample)
Run Code Online (Sandbox Code Playgroud)
输出是:
[0, 1, 2]
[3, 4, 5]
[6]
Run Code Online (Sandbox Code Playgroud)
感谢@thefourtheye和@Bede Constantinides
有一个官方的Python接收器,用于将数组拆分为更小的大小数组的更一般化的情况n.
from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
Run Code Online (Sandbox Code Playgroud)
此代码段来自python itertools doc页面.
这与其他解决方案类似,但速度要快一些。
# Usage: split_half([1,2,3,4,5]) Result: ([1, 2], [3, 4, 5])
def split_half(a):
half = len(a) >> 1
return a[:half], a[half:]
Run Code Online (Sandbox Code Playgroud)