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
#1
Hello guys,
I need a help with my python code and tell me what is wrong here.
tum=[]
    for i in range(400):
        tum.append(i)
    
    rassal=[]
    rassal=random.sample(range(400), 200)


    new_list = [x for x in tum if (x not in rassal)]
    matrix3=[]
    fileHandle.write("[")
    for y in range(80):
        w=[]
        for i in rassal:
               z=5
               w.append(z)
        for k in new_list:
               kk=((round(random.uniform(5,10))))
               w.append(kk)

        matrix3.append(w)
    for w in matrix3:
        w = [str(number) for number in w]
        w = ",".join(w)
        fileHandle.write("["+w+"],\n")
    fileHandle.write("];\n")
What I am trying to do here, I want to create 80 row and 400 coloumn matrix. For column indices , I make a list by randomly generated data and in those indices I want to assign a random value between 5 and 10. For the other values , which hold in rassal list , I want to assign 5. However, in the output for each row first 200 comounb it assings 5, last 200 coloumn random value. This is not what I need . Can you help me what I am doing wrong here ?
Thank you so much
Reply
#2
After reading you code and explanation I still have no clue what you want to accomplish ("For column indices , I make a list by randomly generated data and in those indices I want to assign a random value between 5 and 10. For the other values , which hold in rassal list , I want to assign 5")

I just take first 9 lines of code and reduce it to three for better understanding:

tum = list(range(400))
rassal = random.sample(range(400), 200)
new_list = [x for x in tum if (x not in rassal)]
So you have list of integers 0...399 (tum). Then you take sample from same range (unique numbers as random sample returns unique random elements and range have unique integers) as rassal. And finally you create new_list from numbers which are not in rassal (random sample). This effectively means that you have 200 random unique numbers in new_list.

If you take a moment and think then you can get same result with one line:

new_list = random.sample(range(400), 200)  # get 200 random numbers from range(400)


So please take your time and think little about your problem and then formulate **what** you want to do. After that think **how** could you do it. When writing code try to keep **what** and **how** separate otherwise you will end up with code known under 'spaghetti' moniker - hard to read, understand and debug.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thank you so much @perfringo. I need two of the list. Imagine that we have list tum=[1, 2, 3,4,5,6]. For rassal I generated=[2,5,6] and so newlist will be new_list=[1,3,4].We have 4 rows , so matrix must be something like that
M=R 5 R R 5 5
R 5 R R 5 5
R 5 R R 5 5
R 5 R R 5 5

R means , random numbers assigned. thank you so much
Reply
#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
#5
(Feb-18-2022, 07:32 PM)deanhystad Wrote: 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.

Millions of thanks @deanhystad . I understood my mistake and how elegant your way is. I am just new learner of python. So, sometimes , I have difficulties to think the things in a easier way . Thank you so much again Smile
Reply


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 820 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,199 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,014 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Generate random hex number ZYSIA 1 11,546 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,353 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Random Number Repeating Tzenesh 5 4,023 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Random number generator charlottelol 5 3,210 Nov-10-2020, 10:51 PM
Last Post: deanhystad
  basic random number generator in replace function krug123 2 2,039 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Help with a random number generator dpcalder 2 2,326 Jun-20-2020, 03:50 AM
Last Post: buran
  Generate only one random number for many tries Bhavika 2 1,732 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