![]() |
matrix number assignement to the random indices - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: matrix number assignement to the random indices (/thread-36427.html) |
matrix number assignement to the random indices - juniorcoder - Feb-18-2022 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 RE: matrix number assignement to the random indices - perfringo - Feb-18-2022 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. RE: matrix number assignement to the random indices - juniorcoder - Feb-18-2022 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 RE: matrix number assignement to the random indices - deanhystad - Feb-18-2022 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)) 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.
RE: matrix number assignement to the random indices - juniorcoder - Feb-19-2022 (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: 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 ![]() |