而不是从列表中随机获取两件事,只需随机化列表并对其进行遍历即可创建您指定尺寸的新数组!
import randommy_input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']def random_generator(array,x,y): random.shuffle(array) result = [] count = 0 while count < x: section = [] y1 = y * count y2 = y * (count + 1) for i in range (y1,y2): section.append(array[i]) result.append(section) count += 1 return resultprint random_generator(my_input,3,2)



