根据组号将数组的数组分组

时间:2018-10-24 15:30:06

标签: python python-3.x list grouping nested-lists

我有一个数组数组。我试图将它们分成4组,每组3个。这是代码...

random.shuffle(top_twelve_players_info)
top_twelve_players_info_copy = top_twelve_players_info[:]

group_1 = []
group_2 = []
group_3 = []
group_4 = []

for i in range(3):
    group_1.append(top_twelve_players_info_copy[i])
    top_twelve_players_info_copy.remove(top_twelve_players_info_copy[i])
print(f"Group 1: {group_1}")

for i in range(3):
    group_2.append(top_twelve_players_info_copy[i])
    top_twelve_players_info_copy.remove(top_twelve_players_info_copy[i])
print(f"Group 2: {group_2}")

for i in range(3):
    group_3.append(top_twelve_players_info_copy[i])
    top_twelve_players_info_copy.remove(top_twelve_players_info_copy[i])
print(f"Group 3: {group_3}")

# print(top_twelve_players_info_copy)

for i in range(3):
    group_4.append(top_twelve_players_info_copy[i])
    # top_twelve_players_info_copy.remove(top_twelve_players_info_copy[i])
print(f"Group 4: {group_4}")

当前,top_twelve_players_info看起来像

[
    ['Krombopulos Michael', 10, 3],
    ['Scary Terry', 2, 11],
    ['Jerry Smith', 7, 6],
    ['Blim Blam ', 11, 2],
    ['Summer Smith', 1, 12],
    ['Tophat Jones', 5, 8],
    ['Beth Smith', 6, 7],
    ['Abradolf Lincler', 4, 9],
    ['Alan Rails', 12, 1],
    ['Morty Smith', 3, 10],
    ['Rick Sanchez', 9, 4],
    ['Xenon Bloom', 3, 10]
]

我一直在寻找一种更有效的实现方式,而不是重复执行for循环,甚至根据迭代次数来制作group_1group_2变量。另外,由于某种原因,在当前的实现中,仅当进入第4组时,我才收到错误“列表索引超出范围”。所以我不得不注释掉删除行。为什么会这样?

2 个答案:

答案 0 :(得分:0)

选择python标准库(而不是pandas会提供更优雅的解决方案),您应该这样做:

from collections import defaultdict
groups = defaultdict(list)

players_cnt = 0
for group_no in range(4):
    for player_cnt in range(3):
        selected_player = top_twelve_players_info_copy[players_cnt]
        groups[group_no].append(selected_player)  # here defaultdict makes it more elegant - we do not need to check if group_no is a key in groups

现在,如果确实需要,您可以提取组以分离变量(但我强烈建议将其保存在以组号为键的字典中):

group_1 = groups[1]  # ...etc.

答案 1 :(得分:0)

您得到的错误是因为您在对列表进行变异(从列表中删除项目)时,尝试通过索引遍历列表(因此,当范围循环最后一次到达该值时,索引2不再在那里for循环)。

您可以通过以下方法简化方法:仅使用范围循环对随机混排的列表进行切片,其步长等于要输出的组的大小。以下是一种将结果组放在一个列表中,然后输出类似于您的示例的输出的方法。

import random

top_twelve_players_info = [['Krombopulos Michael', 10, 3], ['Scary Terry', 2, 11], ['Jerry Smith', 7, 6], ['Blim Blam ', 11, 2], ['Summer Smith', 1, 12], ['Tophat Jones', 5, 8], ['Beth Smith', 6, 7], ['Abradolf Lincler', 4, 9], ['Alan Rails', 12, 1], ['Morty Smith', 3, 10], ['Rick Sanchez', 9, 4], ['Xenon Bloom', 3, 10]]

random.shuffle(top_twelve_players_info)
groups = [top_twelve_players_info[i:i+3] for i in range(0, 12, 3)]

for i, group in enumerate(groups):
    print(f'Group {i + 1}: {group}')

# EXAMPLE OUTPUT (differs based on random shuffle results)
# Group 1: [['Beth Smith', 6, 7], ['Scary Terry', 2, 11], ['Krombopulos Michael', 10, 3]]
# Group 2: [['Xenon Bloom', 3, 10], ['Rick Sanchez', 9, 4], ['Tophat Jones', 5, 8]]
# Group 3: [['Morty Smith', 3, 10], ['Abradolf Lincler', 4, 9], ['Jerry Smith', 7, 6]]
# Group 4: [['Summer Smith', 1, 12], ['Alan Rails', 12, 1], ['Blim Blam ', 11, 2]]

或者,如果您确实需要将每个组分配给自己的变量:

[group_1, group_2, group_3, group_4] = [top_twelve_players_info[i:i+3] for i in range(0, 12, 3)]

print(f'Group 1: {group_1}')
print(f'Group 2: {group_2}')
print(f'Group 3: {group_3}')
print(f'Group 4: {group_4}')