how do you replace only a certain number of items in a list randomly?

MR0310
board = []
for x in range(0,8):
    board.append(["0"] * 8)

def print_board(board):
    for row in board:
        print(" ".join(row))

this code creates a grid of zeros but I wish to replace 5 of them with ones and another five with twos

does anyone know a way to do this?

Dani Mesejo

If you want to randomly set some coordinates with "1" and "2", you can do it like this:

import random

board = []
for x in range(0, 8):
    board.append(["0"] * 8)


def print_board(board):
    for row in board:
        print(" ".join(row))


def generate_coordinates(x, y, k):
    coordinates = [(i, j) for i in range(x) for j in range(y)]
    random.shuffle(coordinates)
    return coordinates[:k]


coo = generate_coordinates(8, 8, 10)
ones = coo[:5]
twos = coo[5:]

for i, j in ones:
    board[i][j] = "1"

for i, j in twos:
    board[i][j] = "2"


print_board(board)

Output

0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0
1 0 0 0 2 0 0 0
0 0 0 0 2 0 0 2
2 0 0 0 0 0 0 1

Notes:

  • The code above generates a random sample each time so the output will be different each time (to generate the same use random.seed(42), you can change 42 for any number you want.
  • The function generate_coordinates receives x (number of rows), y (number of columns) and k (the number of coordinates to pick). It generates a sequence of coordinates of x*y, shuffles it and picks the k first.
  • In your specific case x = 8, y = 8 and k = 10 (5 for the ones and 5 for the twos)

Finally, this picks the positions for the ones and twos and changes the values:

ones = coo[:5]
twos = coo[5:]

for i, j in ones:
    board[i][j] = "1"

for i, j in twos:
    board[i][j] = "2"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to randomly select a certain number of elements from a list

From Dev

How do you use Beautiful Soup to pull out list items that have certain class attributes?

From Dev

r: randomly replace certain number of missing values

From Dev

How do you list the folder permissions of only certain directories that follow a common pattern in a particular directory?

From Dev

How do you change bullet/numbered list format of only certain parts?

From Dev

How to randomly replace a string in a list

From Dev

How to Count the Items that Occur a Certain Number of Times in a List

From Dev

How to get number of occurences of items after certain item in list

From Dev

How do you compare only certain bits in data type?

From Dev

How do I replace characters only on certain lines?

From Dev

Perl: How do you sort and convert, items of an array by number of appearances

From Dev

randomly generate a number in c++ but only certain multiples

From Dev

multiplying list of items by a certain number 'x'

From Dev

How to join certain items in list

From Dev

How do you Find & Replace only in specific Styles in Word?

From Dev

Display only certain list items in listview with javascript

From Dev

How do you get Json List items with vbnet

From Dev

How do you print the items in a nested list line by line in Python?

From Dev

How do you get Json List items with vbnet

From Dev

How do you get number frequencies in a list, then index into another list?

From Dev

How do i get an ordered list to randomly spit out only some of the list

From Dev

How do I increase the number of items on a jump list in Windows 10?

From Dev

How do I get a number of items in a filtered list?

From Dev

Randomly display list items

From Dev

Randomly display list items

From Dev

How do I assign a property on only a subrange of items in a list?

From Dev

Adding numbers in a list but only after a certain number

From Dev

List (or move) only files with a certain number of lines?

From Dev

How do you indicate certain elements in list of list and vectors based on list characteristics in R

Related Related

  1. 1

    how to randomly select a certain number of elements from a list

  2. 2

    How do you use Beautiful Soup to pull out list items that have certain class attributes?

  3. 3

    r: randomly replace certain number of missing values

  4. 4

    How do you list the folder permissions of only certain directories that follow a common pattern in a particular directory?

  5. 5

    How do you change bullet/numbered list format of only certain parts?

  6. 6

    How to randomly replace a string in a list

  7. 7

    How to Count the Items that Occur a Certain Number of Times in a List

  8. 8

    How to get number of occurences of items after certain item in list

  9. 9

    How do you compare only certain bits in data type?

  10. 10

    How do I replace characters only on certain lines?

  11. 11

    Perl: How do you sort and convert, items of an array by number of appearances

  12. 12

    randomly generate a number in c++ but only certain multiples

  13. 13

    multiplying list of items by a certain number 'x'

  14. 14

    How to join certain items in list

  15. 15

    How do you Find & Replace only in specific Styles in Word?

  16. 16

    Display only certain list items in listview with javascript

  17. 17

    How do you get Json List items with vbnet

  18. 18

    How do you print the items in a nested list line by line in Python?

  19. 19

    How do you get Json List items with vbnet

  20. 20

    How do you get number frequencies in a list, then index into another list?

  21. 21

    How do i get an ordered list to randomly spit out only some of the list

  22. 22

    How do I increase the number of items on a jump list in Windows 10?

  23. 23

    How do I get a number of items in a filtered list?

  24. 24

    Randomly display list items

  25. 25

    Randomly display list items

  26. 26

    How do I assign a property on only a subrange of items in a list?

  27. 27

    Adding numbers in a list but only after a certain number

  28. 28

    List (or move) only files with a certain number of lines?

  29. 29

    How do you indicate certain elements in list of list and vectors based on list characteristics in R

HotTag

Archive