繁体   English   中英

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

[英]Sort array of arrays into groups based on group number

我有一个数组数组。 我试图将它们分成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组时,我才收到错误“列表索引超出范围”。 所以我不得不注释掉删除行。 这是为什么?

坚持使用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.

您收到的错误是因为您在尝试按索引循环遍历列表时(从列表中删除项目)(因此当范围循环到达最后一个for循环中的值时,索引2不再在那里) 。

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

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}')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM