+ 1

Is there a way to repeat a sequence of numbers for a set amount?

For example, list = [1,2,3,4,5] I want to repeat the items in the list 7 times like so (1,2,3,4,5,1,2). Is this possible?

20th Oct 2018, 2:38 AM
CodeforLife1133
CodeforLife1133 - avatar
6 odpowiedzi
+ 5
from itertools import cycle def repeat(lst, times): c = cycle(lst) for i in range(times): yield next(c) print(*repeat([1,2,3,4,5], 7))
20th Oct 2018, 7:08 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 4
numbers = [1, 2, 3, 4, 5] for i in range(7): print (numbers[0]) numbers.append (numbers.pop(0))
20th Oct 2018, 7:35 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 3
from itertools import cycle numbers = cycle([1, 2, 3, 4, 5]) for i in range(7): print (next(numbers))
20th Oct 2018, 7:42 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 2
Jan Markus Well, that's how I understood the question. How about this then? a = [1,2,3,4,5] n = 7 print((a * (n // len(a) + 1))[:n]) #[1,2,3,4,5,1,2]
20th Oct 2018, 10:08 AM
Anna
Anna - avatar
0
list+list[:7-len(list)]?
20th Oct 2018, 4:08 AM
Александр Лебедев
Александр Лебедев - avatar
- 1
What are you trying to do? The easiest way would be a=[1,2,3,4,5] print(7 * a) You can also use also use this to iterate over the list: for n in 7 * a: print(n)
20th Oct 2018, 7:00 AM
Anna
Anna - avatar