Python Forum
Repeating same number in sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repeating same number in sequence
#1
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
Reply
#2
Welcome!

You can do it in a second for loop inside the first one:
for number in range(5):
    # loop 3 times here
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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??
Reply
#4
You can do this with a generator very easy.
You have to use two nested loops.

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 _.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(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.
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..
Reply
#6
(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.
[Image: Sl3qbO.jpg]
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 692 Dec-12-2023, 09:09 AM
Last Post: buran
  repeating a user_input astral_travel 17 2,265 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,492 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,239 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Random Number Repeating Tzenesh 5 4,022 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  print two different sequence number mantonegro 2 1,659 Nov-16-2020, 06:19 PM
Last Post: mantonegro
  factorial, repeating Aldiyar 4 2,792 Sep-01-2020, 05:22 PM
Last Post: DPaul
  Sequence Generator - Long number krux_rap 3 2,314 Aug-19-2020, 06:37 PM
Last Post: buran
  Longest sequence of repeating integers in a numpy array Cricri 5 5,789 Jun-08-2020, 06:48 AM
Last Post: Cricri
  number repeating twice in loop JonnyEnglish 3 3,308 Nov-24-2019, 09:23 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020