Python Forum
matrix number assignement to the random indices
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matrix number assignement to the random indices
#4
I do not see purpose of tum or new_list. Essentially new_list is "not rassal". new_list has a use if you were doing this:
row = [5]*COLUMNS
for i in new_list:
    row[i] = random.whatever()
But how is that any different than this?
row = [5]*COLUMNS
for i in rassal:
    row[i] = random.whatever()
rassal is just a random selection. How is one random selection different from another?
I would solve the problem like this
import random

COLUMNS = 10
ROWS = 4
rassal = set(random.sample(range(COLUMNS), k=COLUMNS//2))
matrix = [[5 if c in rassal else random.randint(5, 10) for c in range(COLUMNS)] for _ in range(ROWS)]

print(rassal)
for row in matrix:
    values = [f"{value:>2}" for value in row]
    print(", ".join(values))
Output:
[1, 2, 5, 6, 9] 10, 5, 5, 8, 10, 5, 5, 7, 10, 5 10, 5, 5, 5, 7, 5, 5, 6, 7, 5 9, 5, 5, 5, 6, 5, 5, 9, 9, 5 6, 5, 5, 10, 8, 5, 5, 7, 9, 5
About your code. In your code you first add a bunch of 5's to w, then you add a bunch of random numbers to w. You never use the contents of rassal or new_list, you only use their length.
juniorcoder likes this post
Reply


Messages In This Thread
RE: matrix number assignement to the random indices - by deanhystad - Feb-18-2022, 07:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 899 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,440 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,132 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Generate random hex number ZYSIA 1 11,846 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,423 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Random Number Repeating Tzenesh 5 4,125 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Random number generator charlottelol 5 3,302 Nov-10-2020, 10:51 PM
Last Post: deanhystad
  basic random number generator in replace function krug123 2 2,088 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Help with a random number generator dpcalder 2 2,419 Jun-20-2020, 03:50 AM
Last Post: buran
  Generate only one random number for many tries Bhavika 2 1,798 Mar-29-2020, 12:12 PM
Last Post: Bhavika

Forum Jump:

User Panel Messages

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