Python Forum
Preventing Duplicate Placement in 2D Array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preventing Duplicate Placement in 2D Array
#2
Create a list of tuples for all possible combinations of row and col. Shuffle the list or randomly remove combinations from the list. This example shuffles the fields list and pops the combinations off the end.
from numpy import zeros, sum
import random
import itertools

N = 25 # number of people
A = 10 # length of field
B = 10 # width of field
D = 3 # columns for directory
directory = zeros((N,D),float)
# Create an entry for each row/column combination
fields = list(itertools.product(range(A), range(B)))
random.shuffle(fields)

for i in range(N):
    directory[i,0] = i
    directory[i,1], directory[i,2] = fields.pop()

print(directory)
Reply


Messages In This Thread
RE: Preventing Duplicate Placement in 2D Array - by deanhystad - Feb-03-2022, 10:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Data placement nboogerz 1 1,957 May-16-2020, 11:20 AM
Last Post: snippsat
  Automated Bet placement redmercury 2 8,514 Dec-04-2019, 10:53 AM
Last Post: redmercury
  Preventing: IndexError: list index out of range PappaBear 1 6,606 Jun-03-2019, 05:50 PM
Last Post: SheeppOSU
  Preventing useless multiple disk writes Steffenwolt 9 4,795 Jul-28-2018, 06:39 PM
Last Post: gontajones
  Question with while loop placement Tunechi 2 3,106 May-16-2018, 02:54 AM
Last Post: Tunechi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020