Feb-18-2022, 07:32 PM
(This post was last modified: Feb-18-2022, 07:32 PM by deanhystad.)
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:
I would solve the problem like 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.