Questions tagged [repeat]

"repeat" refers to the action of doing something over again.

Note that this tag can often be replaced by the more specific or tags.

3161 questions
842
votes
24 answers

Repeat a string in JavaScript a number of times

In Perl I can repeat a character multiple times using the syntax: $a = "a" x 10; // results in "aaaaaaaaaa" Is there a simple way to accomplish this in Javascript? I can obviously use a function, but I was wondering if there was any built in…
Steve
  • 53,375
  • 33
  • 96
  • 141
614
votes
25 answers

Create an array with same element repeated multiple times

In Python, where [2] is a list, the following code gives this output: [2] * 5 # Outputs: [2,2,2,2,2] Does there exist an easy way to do this with an array in JavaScript? I wrote the following function to do it, but is there something shorter or…
Curious2learn
  • 31,692
  • 43
  • 108
  • 125
254
votes
15 answers

Repeat string to certain length

What is an efficient way to repeat a string to a certain length? Eg: repeat('abc', 7) -> 'abcabca' Here is my current code: def repeat(string, length): cur, old = 1, string while len(string) < length: string += old[cur-1] cur…
John Howard
  • 61,037
  • 23
  • 50
  • 66
153
votes
3 answers

vim repeat find next character 'x'

I often navigate in vim by f x to find next occurrence of character 'x', but overlook that there is a word (or more words) containing 'x' in between the word I want to edit and the beginning cursor position. So i have to f x again, which is kind of…
epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
130
votes
8 answers

Do something every x minutes in Swift

How can I run a function every minute? In JavaScript I can do something like setInterval, does something similar exist in Swift? Wanted output: Hello World once a minute...
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67
123
votes
10 answers

Repeat rows of a data.frame

I want to repeat the rows of a data.frame, each N times. The result should be a new data.frame (with nrow(new.df) == nrow(old.df) * N) keeping the data types of the columns. Example for N = 2: A B C A B C 1…
Stefan
  • 1,530
  • 2
  • 11
  • 9
102
votes
3 answers

Create sequence of repeated values, in sequence?

I need a sequence of repeated numbers, i.e. 1 1 ... 1 2 2 ... 2 3 3 ... 3 etc. The way I implemented this was: nyear <- 20 names <- c(rep(1,nyear),rep(2,nyear),rep(3,nyear),rep(4,nyear), …
Wesley Burr
  • 1,224
  • 3
  • 9
  • 9
97
votes
5 answers

How to capture an arbitrary number of groups in JavaScript Regexp?

I would expect this line of JavaScript: "foo bar baz".match(/^(\s*\w+)+$/) to return something like: ["foo bar baz", "foo", " bar", " baz"] but instead it returns only the last captured match: ["foo bar baz", " baz"] Is there a way to get all the…
disc0dancer
  • 9,185
  • 11
  • 36
  • 46
78
votes
7 answers

How to repeat a Pandas DataFrame?

This is my DataFrame that should be repeated for 5 times: >>> x = pd.DataFrame({'a':1,'b':2}, index = range(1)) >>> x a b 0 1 2 I want to have the result like this: >>> x.append(x).append(x).append(x) a b 0 1 2 0 1 2 0 1 2 0 1 …
lsheng
  • 3,539
  • 10
  • 33
  • 43
75
votes
4 answers

do-while loop in R

I was wondering about how to write do-while-style loop? I found this post: you can use repeat{} and check conditions whereever using if() and exit the loop with the "break" control word. I am not sure what it exactly means. Can someone please…
Tim
  • 1
  • 141
  • 372
  • 590
75
votes
4 answers

Repeat and concatenate a string N times

In Ruby I could repeat a String n times with the following: E.G. "my_string" * 2 -> "my_stringmy_string" Is there an equally simple way for doing this in R?
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
70
votes
4 answers

Using explicitly numbered repetition instead of question mark, star and plus

I've seen regex patterns that use explicitly numbered repetition instead of ?, * and +, i.e.: Explicit Shorthand (something){0,1} (something)? (something){1} (something) (something){0,} (something)* (something){1,} …
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
68
votes
10 answers

How can I replicate rows of a Pandas DataFrame?

My pandas dataframe looks like this: Person ID ZipCode Gender 0 12345 882 38182 Female 1 32917 271 88172 Male 2 18273 552 90291 Female I want to replicate every row 3 times and reset the index to get: Person ID …
DasVisual
  • 779
  • 2
  • 7
  • 9
68
votes
6 answers

How can I repeat a string N times in Perl?

In Python, if I do this: print "4" * 4 I get > "4444" In Perl, I'd get > 16 Is there an easy way to do the former in Perl?
izb
  • 50,101
  • 39
  • 117
  • 168
66
votes
5 answers

How do I repeat any command in Vim, like "C-x z" in emacs?

In Vim, is there any way to repeat the last command regardless of whether it was an edit or not, and without having the foresight to first record a macro? E.g. say I type :bn, and want to do it again (it was the wrong file). Pressing . obviously…
johv
  • 4,424
  • 3
  • 26
  • 42
1
2 3
99 100