0
[SOLVED] Random for-loop in Python
If I have a list of 5 elements and I want to print 5 times the same list with for loop, how can I do ? Code like this: List = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’] Var = random.choices(list, k=5) For i in var: Print(i) Output 2<—— starting from this line 1 4 <——- 5 times like this line 3 5 but not in the same order like: print(i * 5)
37 Respostas
+ 2
Life_is_ligal🤦 👑 Tchybooxuur! S.Hossein Motaharpour this code was what i need:
1 input = 1 vertical line
2 input = 2 vertical line
And all line are in different random sequence
https://code.sololearn.com/cqPhcyNT6gTE/?ref=app
+ 6
Nicola Esu ,
please check this:
from random import choices
lst = ['1', '2', '3', '4', '5']
for num in choices(lst, k=25):
print(num * 5)
in this sample it can happen, that not every number from the list is taken. if you want to get all numbers (out of the list) in each run, we have to go for a different approach
+ 5
Nicola Esu ,
still not quite clear what you really expect. we need a clear task description with output samples. otherwise it is more or less a guess!
thanks for your help!
+ 4
Nicola Esu
You do realize that the `random` module has a `shuffle` function, right?
example = [ 1, 2, 3, 4, 5 ]
random.shuffle(example)
print(example)
: [ 4, 1, 5, 2, 3 ]
+ 3
There's definitely a language barrier here, I believe. What is your strongest spoken language?
+ 3
Like this?
5 5
4 4
3 3
2 2
1 1
+ 2
Suppose, we have 2 random sequence like
a = [5,4,3,2,1]
b = [3,2,1,5,4]
Then you want do print it like:
5 3
4 2
3 1
2 5
1 4
Is this what you try to achieve ("display vertically")?
+ 2
But that's what I did in my first post? Same list, different orders, vertically displayed?
+ 2
Nicola Esu
I wasn't talking about programming languages.
I was talking about spoken (verbal) languages ... English, Spanish, French, Hindi, Japanese, etc.
If you are more fluent when speaking a language other than English, don't be afraid to communicate in that language - especially since I (and many others) can't understand what you're asking 90% of the time.
P.S. The phrase "the same random sequence" *still* doesn't make any sense. Lol. Js.
+ 2
Nicola Esu
It's pretty hard trying to understand the question. If you could be more explicit, it'd help immensely.
+ 1
Lothar if i use print(i*5)
This is the output of the code:
2 2 2 2 2
1 1 1 1 1 1
4 4 4 4 4
3 3 3 3 3
5 5 5 5 5
And tha output that i want is 5 times but not in the same random sequence.
+ 1
Fox i want print 5 times not the same random sequence, but starting from the same line like this:
First. Second. Third.
2. 1. 5.
4. 3. 2.
5. 4. 1
+ 1
Nicola Esu
https://stackoverflow.com/questions/48053979
Is this one of the goals you're referring to?
Pretty sure you could also approach it using matrix operations, etc.
+ 1
Nicola Esu
That doesn't tell me very much, lol. I mean, I can think of a different approach, but choosing not to use simple/optimal strategies is .. not very wise in programming.
+ 1
Maybe this? I used numpy instead if random, the sequences are in a matrix
https://code.sololearn.com/cI6y4veEX2As/?ref=app
+ 1
not clear question!
+ 1
Fox Lisa Lothar Win Htay 🇲🇲 Delicate Cat
this was the result I wanted, now a question arises, if my input should reach 15 characters, in the print I have to write the variable 10 times or is there a way not to do it?
https://code.sololearn.com/cqPhcyNT6gTE/?ref=app
+ 1
First part of this code print list 5 times in 5 order:
https://code.sololearn.com/c79tv6qTzf0F/?ref=app
But if you want to change number of numbers that print in each line you can do this:
https://code.sololearn.com/coLO9QOYc6qj/?ref=app
+ 1
At end if you want print specific list parallel you can use zip function
https://code.sololearn.com/cVp8CKz1xp7W/?ref=app
+ 1
55
44
33
22
11
Like thise