Posts: 33
Threads: 13
Joined: Jul 2017
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
Posts: 1,950
Threads: 8
Joined: Jun 2018
Feb-18-2022, 02:56 PM
(This post was last modified: Feb-18-2022, 02:57 PM by perfringo.)
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.
Posts: 33
Threads: 13
Joined: Jul 2017
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
Posts: 6,779
Threads: 20
Joined: Feb 2020
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:
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
Posts: 33
Threads: 13
Joined: Jul 2017
(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
|