Posts: 6
Threads: 2
Joined: Oct 2018
Hi all...
Sorry if my question already been asked so many times..
I tried to google it but not on my luck i think...
Im very new about python i hope i can explain well .
So here my question..
Lets say i want to repeating the number from 0 to 5, and i want to each numbers have 3 time repeating before it change to new number.. eg:
0
0
0
1
1
1
2
2
2
What topic should i search about this?
Or do you the code about this thing so i can learn?
Thanks
Posts: 2,955
Threads: 48
Joined: Sep 2016
Welcome!
You can do it in a second for loop inside the first one:
for number in range(5):
# loop 3 times here
Posts: 6
Threads: 2
Joined: Oct 2018
Oct-28-2018, 07:08 AM
(This post was last modified: Oct-28-2018, 07:08 AM by Rudinirudini.)
(Oct-28-2018, 06:49 AM)wavic Wrote: Welcome!
You can do it in a second for loop inside the first one:
for number in range(5):
# loop 3 times here
thanks for answering my question..
i tried to copy paste your code but here is what happened
>>> for number in range(5):
# loop 3 times here
print (number)
0
1
2
3
4 when i tried put the number in line 2 it is what happened..
for i in range (5):
3
print (i)
3
0
3
1
3
2
3
3
3
4 only number 3 which repeated
what should i need to put in line 2??
Posts: 2,016
Threads: 9
Joined: May 2017
Oct-28-2018, 07:22 AM
(This post was last modified: Oct-28-2018, 07:23 AM by DeaD_EyE.)
You can do this with a generator very easy.
You have to use two nested loops.
def repeater(iterable, times):
for element in iterable:
for _ in range(times):
yield element
list(repeater(range(3), 3)) Output: [0, 0, 0, 1, 1, 1, 2, 2, 2]
The _ is used a a placeholder for a throw away variable.
The outer loop iterates over the iterable (list, tuple, etc) and the inner loop is for repeating the value.
The number is not needed, this is the cause why I use the _ .
Posts: 6
Threads: 2
Joined: Oct 2018
(Oct-28-2018, 07:22 AM)DeaD_EyE Wrote: You can do this with a generator very easy. You have to use two nested loops. def repeater(iterable, times): for element in iterable: for _ in range(times): yield element list(repeater(range(3), 3)) Output: [0, 0, 0, 1, 1, 1, 2, 2, 2]
The _ is used a a placeholder for a throw away variable. The outer loop iterates over the iterable (list, tuple, etc) and the inner loop is for repeating the value. The number is not needed, this is the cause why I use the _ .
i still dont get it Mr DeaD_EyE..
i put your code in my shell like this..
>>> def repeater(iterable, times):
for element in iterable:
for _ in range(times):
yield element
list(repeater(range(3), 3))
and i dont get anything just blank..
do i need change something for your code??
*im sorry if im slow to understand..
Posts: 7,081
Threads: 122
Joined: Sep 2016
Oct-28-2018, 11:57 AM
(This post was last modified: Oct-28-2018, 11:57 AM by snippsat.)
(Oct-28-2018, 07:35 AM)Rudinirudini Wrote: i put your code in my shell like this.. You can not put all code in interactive shell.
This code run you as a script.
def repeater(iterable, times):
for element in iterable:
for _ in range(times):
yield element
print(list(repeater(range(3), 3))) Output: [0, 0, 0, 1, 1, 1, 2, 2, 2]
You run it shell but then you have to Enter after each line.
>>> def repeater(iterable, times):
... for element in iterable:
... for _ in range(times):
... yield element
...
>>> list(repeater(range(3), 3))
[0, 0, 0, 1, 1, 1, 2, 2, 2] Better shell/REPL like IPython, ptpython can paste in whole code into shell an it will run.
Posts: 2,016
Threads: 9
Joined: May 2017
Oct-28-2018, 07:44 PM
(This post was last modified: Oct-28-2018, 07:44 PM by DeaD_EyE.)
Ok, you need to know what a Generator is.
Additionally you can visualize your code: https://goo.gl/ri98UM
By the way, you did not see anything, because you run your program not in a REPL (python -i programm.py or ipython program.py)
I have forgotten to print it.
|