myList = list(range(15))
    n = 3
    print([myList[i:i + n] for i in range(0, len(myList), n)])
 

[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]]

看了好多都是下面这种写法,不知道是谁复制谁的,还是自己的方便 myList = list(range(15)) n = 3 print([myList[i:i + n] for i in range(0, len(myList), n)])[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]]...
如果我们需要将一个列表按指定数目分多个列表:比如[1,2,3,4,5,6,7,8,9,10]分[1,2,3][4,5,6][7,8,9][10],我们可以建立一个列表分割的函数split_list.py。 def list_of_groups(init_list, children_list_len): list_of_groups = zip(*(iter(init_list),) *children_list_len) end_list = [list(i) for i in list_of_groups] count = len(init_list) % childre
如果我们需要将一个列表按指定数目分多个列表:比如[1,2,3,4,5,6,7,8,9,10]分[1,2,3][4,5,6][7,8,9][10],我们可以建立一个列表分割的函数split_list.py。def list_of_groups(init_list, children_list_len): list_of_groups = zip(*(iter(init_list),) *...
names=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] n=3 #将列表每3个组一个小列表, for i in range(0, len(names), n): name=names[i:i + n] print(name) 上面的例子是正好是分割间隔的整数倍,分割完毕 但如果不是分割间隔的整数倍时,上面的代码能否奏效呢? names=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] n=3 #将列表每3个
def list_of_groups(init_list, childern_list_len): list_of_groups = zip(*(iter(init_list),) *childern_list_len) end_list = [list(i) for i in list_of_groups] count = le list.split() 就是以字符串或列表里某个字符把字符串或列表进行分割新的字符串或列表。 list.split(“,”) 这就是以“,”来分割,如果原来字符串或列表里没有“,” 则无法功。 引用网上一个例子更经典!!!! str="hello boy<[www.doiido.com]>byebye" str.s... wandb.errors.UsageError: api_key not configured (no-tty). call wandb.login(key=[your_api_key]) BrokenPipeError: [Errno 32] Broken pipe Error caught was: No module named ‘triton‘