根据组编号将数组排序为组

2024-07-06 18:31:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数组。我试着把他们分成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个组时,才会出现错误“list index out of range”。所以我不得不把删除线注释掉。为什么?你知道吗


Tags: ininfofortopgrouprange数组remove
2条回答

得到的错误是因为您在尝试按索引遍历列表时正在对列表进行变异(从列表中删除项)(因此,当范围循环到达上一个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}')

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

相关问题 更多 >