【问题标题】:Sort array of arrays into groups based on group number根据组号将数组数组排序为组
【发布时间】:2018-10-24 15:30:05
【问题描述】:

我有一个数组数组。我试图将它们分成 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 python-3.x list grouping nested-lists


    【解决方案1】:

    您遇到的错误是因为您在尝试按索引循环遍历列表时正在改变列表(从中删除项目)(因此当您的范围循环在最后一次到达该值时,索引 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}')
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这能确保我在组中没有重复项吗?就像'Harry' 也属于第 1 组和第 2 组?这就是我使用“删除”功能的原因
      • 只要列表只包含唯一名称,组中就不会有任何重复。
      猜你喜欢
      • 1970-01-01
      • 2017-03-18
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2015-04-08
      • 2023-02-24
      • 1970-01-01
      相关资源
      最近更新 更多