Python - 通过限制子数组中的元素数来对数组进行分组

时间:2014-02-23 08:28:28

标签: python arrays list grouping

m = [
    ('Mon', [1,2,4,32,23]),
    ('Tue', [1,7]),
    ('Wed', [11,9,6,4,32,1]),
    ('Thu', [8]),
    ('Fri', [344,3,2,0])
]

如何按如下方式对数组进行分组:

l = 3
m = [
    [('Mon', [1,2,4])],

    [('Mon', [32,23]),
    ('Tue', [1])],

    [('Tue', [7]),
    ('Wed', [11,9])],

    [('Wed', [6,4,32])],

    [('Wed', [1]),
    ('Thu', [8]),
    ('Fri', [344])],

    [('Fri', [3,2,0])]
]

#######

l = 10
m = [
    [('Mon', [1,2,4,32,23]), 
    ('Tue', [1,7]),
    ('Wed', [11,9,6])],

    [('Wed', [4,32,1]),
    ('Thu', [8]),
    ('Fri', [344,3,2,0])]
]

谢谢!

5 个答案:

答案 0 :(得分:2)

这有效:

from itertools import islice
from pprint import pprint

def split(seq, l):
    count = sum(len(x[1]) for x in m)
    seq = [(a, iter(b)) for a, b in seq]
    while count > 0:
        group = []
        current_count = 0
        for x in seq:
            data = list(islice(x[1], l-current_count))
            count -= len(data)
            if data:
                if current_count  + len(data) > l:
                    yield group
                    group = [(x[0], data)]
                    current_count = len(data)
                    break
                else:
                    group.append((x[0], data))
                    current_count += len(data)
        yield group

out1 = list(split(m, 3))
out2 = list(split(m, 10))
pprint(out1)
pprint(out2)

<强>输出:

[[('Mon', [1, 2, 4])],
 [('Mon', [32, 23]), ('Tue', [1])],
 [('Tue', [7]), ('Wed', [11, 9])],
 [('Wed', [6, 4, 32])],
 [('Wed', [1]), ('Thu', [8]), ('Fri', [344])],
 [('Fri', [3, 2, 0])]]

[[('Mon', [1, 2, 4, 32, 23]), ('Tue', [1, 7]), ('Wed', [11, 9, 6])],
 [('Wed', [4, 32, 1]), ('Thu', [8]), ('Fri', [344, 3, 2, 0])]]

答案 1 :(得分:1)

m = [
    ('Mon', [1,2,4,32,23]),
    ('Tue', [1,7]),
    ('Wed', [11,9,6,4,32,1]),
    ('Thu', [8]),
    ('Fri', [344,3,2,0])
]

maxsize = 3

output = []
for day, arr in m:
    thelist = []
    for num in arr:
        thelist.append(num)
        if len(thelist)==maxsize:
            output.append( (day, thelist) )
            thelist = []
    if len(thelist):
        output.append( (day, thelist) )


import pprint
pprint.pprint(output)

答案 2 :(得分:1)

>>> from itertools import groupby, izip
>>> from operator import itemgetter

>>> i = 3

>>> weekday_number = ((weekday, number) for (weekday, numbers) in m for number in numbers)
>>> grouped_by_i_elements = izip(*[weekday_number]*i)
>>> [[(key, [number for weekday,number in elements_it]) for key,elements_it in groupby(group, itemgetter(0))] for group in grouped_by_i_elements]
[[('Mon', [1, 2, 4])],
 [('Mon', [32, 23]), ('Tue', [1])],
 [('Tue', [7]), ('Wed', [11, 9])],
 [('Wed', [6, 4, 32])],
 [('Wed', [1]), ('Thu', [8]), ('Fri', [344])],
 [('Fri', [3, 2, 0])]]

答案 3 :(得分:0)

m = [
    ('Mon', [1,2,4,32,23]),
    ('Tue', [1,7]),
    ('Wed', [11,9,6,4,32,1]),
    ('Thu', [8]),
    ('Fri', [344,3,2,0])
]

# at first we change the data structure like this
tuple_list = [ (day, number) for day, numbers in m for number in numbers ]

tuple_list
Out[22]:
[('Mon', 1),
 ('Mon', 2),
 ('Mon', 4),
...
 ('Fri', 3),
 ('Fri', 2),
 ('Fri', 0)]

# now we can group l of them
l = 3
subgroups = [ tuple_list[e:e+l] for e in range(0,len(tuple_list),l)]
subgroups
Out[23]:
[[('Mon', 1), ('Mon', 2), ('Mon', 4)],
 [('Mon', 32), ('Mon', 23), ('Tue', 1)],
 [('Tue', 7), ('Wed', 11), ('Wed', 9)],
 [('Wed', 6), ('Wed', 4), ('Wed', 32)],
 [('Wed', 1), ('Thu', 8), ('Fri', 344)],
 [('Fri', 3), ('Fri', 2), ('Fri', 0)]]
In [24]:

# To get the old data structure, we can group each subgroup
# by its day of week
from itertools import groupby
from operator import itemgetter
subgroups_b = [ 
                [
                 (key, [number for day, number in grouper])
                      for key, grouper in groupby(subgroup, key=itemgetter(0))
                ]
                for subgroup in subgroups 

            ]
subgroups_b
Out[25]:
[[('Mon', [1, 2, 4])],
 [('Mon', [32, 23]), ('Tue', [1])],
 [('Tue', [7]), ('Wed', [11, 9])],
 [('Wed', [6, 4, 32])],
 [('Wed', [1]), ('Thu', [8]), ('Fri', [344])],
 [('Fri', [3, 2, 0])]]

答案 4 :(得分:0)

这两个功能将解决问题:

import itertools

def custom_len(list_of_tuples):
    tmp_l = [t[1] for t in list_of_tuples]
    return len(list(itertools.chain(*tmp_l)))

def group_in_lists(list_of_tuples, max_list_size):
    tmp_list = []
    for x in list_of_tuples:
        tmp_list.append((x[0], []))

        for y in x[1]:
            if custom_len(tmp_list) >= max_list_size:
                yield list(tmp_list)
                tmp_list = [(x[0], [y])]
            else:
                tmp_list[-1][1].append(y)

        if custom_len(tmp_list) >= max_list_size:
            yield list(tmp_list)
            tmp_list = []

    if tmp_list != []:
        yield tmp_list

测试功能:

m = [
    ('Mon', [1,2,4,32,23]),
    ('Tue', [1,7]),
    ('Wed', [11,9,6,4,32,1]),
    ('Thu', [8]),
    ('Fri', [344,3,2,0])
]
l = 3

test_output = group_in_lists(m, l)

import pprint
pprint.pprint(list(test_output))