將陣列list等分成幾組 python

2021-09-28 21:12:38 字數 4026 閱讀 2618

問題描述:

給定乙個陣列,將其等分成n組,最後一組元素不足也作為一組。

例如:將陣列test_list= [3, 4, 5, 6, 7, 22, 35, 46, 78, 100],等分成陣列:alist= [[3, 4, 5], [6, 7, 22], [35, 46, 78], [100]]

對於上述問題的反向操作,也就是,給定陣列alist,將其合併合併成乙個陣列test_list,python的內建庫itertoolschain函式可實現改操作。

in [1]

:import itertools

...: alist =[[

3,4,

5],[

6,7,

22],[

35,46,

78],[

100]].

..:print

list

(itertools.chain(alist[0]

, alist[1]

))..

.: len_a =

len(alist)..

.: new_list =

...:

for i in

range

(len_a):.

..: new_list =

list

(itertools.chain(new_list, alist[i]))

...:

print new_list

...:[3,

4,5,

6,7,

22][3

,4,5

,6,7

,22,35

,46,78

,100

]

下面是實現上述問題的**。若將陣列等分成n組,每組元素至少m個,其中 m = len_list / n的上取整(用到math模組中的ceil)。因此,該陣列每當下標整除m時,在新陣列中增加乙個陣列,不能整除時,就在增加的陣列後新增元素。

in [2]

:import math

...: test_list =[3

,4,5

,6,7

,22,35

,46,78

,100].

..: len_list =

len(test_list)..

.: n =3.

..: m = math.ceil(len_list /

float

(n))..

.: alist =

...: group_m =-1

...:

for i in

range

(len_list):.

..:if i % m ==0:

...: group_m +=1.

..[test_list[i]])

...:

print

"%s: %s"

%(i, alist)..

.:else:.

..: alist[group_m]).

..:print

"%s: %s"

%(i, alist)..

.:print

"split list:"

, alist

...:0:[

[3]]

1:[[

3,4]

]2:[

[3,4

,5]]

3:[[

3,4,

5,6]

]4:[

[3,4

,5,6

],[7

]]5:

[[3,

4,5,

6],[

7,22]

]6:[

[3,4

,5,6

],[7

,22,35

]]7:

[[3,

4,5,

6],[

7,22,

35,46]

]8:[

[3,4

,5,6

],[7

,22,35

,46],

[78]]

9:[[

3,4,

5,6]

,[7,

22,35,

46],[

78,100]

]split list:[

[3,4

,5,6

],[7

,22,35

,46],

[78,100

]]

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import math

deflst_trans0

(test_list, n)

:"""n: split list into `n` groups

"""len_list =

len(test_list)

m = math.ceil(len_list /

float

(n))

alist =

group_m =-1

for i in

range

(len_list)

:if i % m ==0:

group_m +=

1[test_list[i]])

else

: alist[group_m]

)return alist

deflst_trans1

(lst, n)

: m =

int(math.ceil(

len(lst)

/float

(n))

) sp_lst =

for i in

range

(n):

(i+1

)*m]

)return sp_lst

# 可以不使用math模組

deflst_trans2

(lst, n):if

len(lst)

% n !=0:

m =(len

(lst)

// n)+1

else

: m =

len(lst)

// n

sp_lst =

for i in

range

(n):

(i+1

)*m]

)return sp_lst

if __name__ ==

'__main__'

: test_list =[3

,4,5

,6,7

,22,35

,46,78

,100

]print

(lst_trans0(test_list,3)

)print

(lst_trans1(test_list,3)

)print

(lst_trans2(test_list,3)

)

執行結果

$ python test_split.py

[[3, 4, 5, 6], [7, 22, 35, 46], [78, 100]][

[3, 4, 5, 6], [7, 22, 35, 46], [78, 100]][

[3, 4, 5, 6], [7, 22, 35, 46], [78, 100]

]

(完)

PHP extract 將陣列拆分成多個變數的函式

extract 函式語法 int extract array var array int extract type extr overwrite string prefix 功能 extract 函式提取關聯陣列 對數字索引陣列無效 每對key和value,生成以key為變數名 value為對應值的...

字串拆分成陣列 842將陣列拆分成斐波那契數列

這個 星期六要考四級,真的是花36塊錢體驗下卷子。上次去華南理工比數學競賽,沒想到成功混了乙個一等獎 首先來看看題目 給定乙個數字字串 s,比如 s 123456579 我們可以將它分成斐波那契式的序列 123,456,579 形式上,斐波那契式序列是乙個非負整數列表 f,且滿足 0 f i 2 3...

Leetcode 842 將陣列拆分成斐波那契序列

給定乙個數字字串 s,比如 s 123456579 我們可以將它分成斐波那契式的序列 123,456,579 形式上,斐波那契式序列是乙個非負整數列表 f,且滿足 0 f i 2 31 1,也就是說,每個整數都符合 32 位有符號整數型別 f.length 3 對於所有的0 i f.length 2...