repeat

Repeat string to certain length

倖福魔咒の 提交于 2019-11-26 07:23:20
问题 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 = (cur+1)%len(old) return string Is there a better (more pythonic) way to do this? Maybe using list comprehension? 回答1: def repeat_to_length(string_to_expand, length): return (string_to_expand * ((length/len(string_to_expand))+1))[:length] For python3: def repeat_to

Repeating each element of a numpy array 5 times

╄→гoц情女王★ 提交于 2019-11-26 06:43:06
问题 import numpy as np data = np.arange(-50,50,10) print data [-50 -40 -30 -20 -10 0 10 20 30 40] I want to repeat each element of data 5 times and make new array as follows: ans = [-50 -50 -50 -50 -50 -40 -40 ... 40] How can I do it? What about repeating the whole array 5 times? ans = [-50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 -50 -40 -30 -20 -10 0 10 20 30 40 .......] 回答1: In [1]: data = np.arange(-50

Repeat rows of a data.frame [duplicate]

旧城冷巷雨未停 提交于 2019-11-26 03:09:53
问题 This question already has an answer here: Repeat rows of a data.frame N times 7 answers 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 j i 100 1 j i 100 --> 2 j i 100 2 K P 101 3 K P 101 4 K P 101 So, each row is repeated 2 times and characters remain characters, factors remain factors, numerics remain numerics, ... My first attempt

Create an array with same element repeated multiple times

孤街浪徒 提交于 2019-11-26 02:17:36
问题 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 better? var repeatelem = function(elem, n){ // returns an array with element elem repeated n times. var arr = []; for (var i = 0; i <= n; i++) { arr = arr.concat(elem); }; return arr; }; 回答1: You can do it like this: function fillArray(value, len) { if

Element-wise array replication in Matlab

让人想犯罪 __ 提交于 2019-11-26 00:29:13
问题 Let\'s say I have a one-dimensional array: a = [1, 2, 3]; Is there a built-in Matlab function that takes an array and an integer n and replicates each element of the array n times? For example calling replicate(a, 3) should return [1,1,1,2,2,2,3,3,3] . Note that this is not at all the same as repmat . I can certainly implement replicate by doing repmat on each element and concatenating the result, but I am wondering if there is a built in function that is more efficient. 回答1: As of R2015a ,

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

戏子无情 提交于 2019-11-25 23:35:12
问题 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 captured matches? 回答1: When you repeat a capturing group, in most flavors, only the last capture is kept; any previous capture is overwritten. In some flavor, e.g. .NET, you can get all intermediate captures, but this is not the case

Repeat copies of array elements: Run-length decoding in MATLAB

早过忘川 提交于 2019-11-25 23:17:15
问题 I\'m trying to insert multiple values into an array using a \'values\' array and a \'counter\' array. For example, if: a=[1,3,2,5] b=[2,2,1,3] I want the output of some function c=somefunction(a,b) to be c=[1,1,3,3,2,5,5,5] Where a(1) recurs b(1) number of times, a(2) recurs b(2) times, etc... Is there a built-in function in MATLAB that does this? I\'d like to avoid using a for loop if possible. I\'ve tried variations of \'repmat()\' and \'kron()\' to no avail. This is basically Run-length

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

≡放荡痞女 提交于 2019-11-25 21:58:44
问题 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,} (something)+ The questions are: Are these two forms identical? What if you add possessive/reluctant modifiers? If they are identical, which one is more idiomatic? More readable? Simply \"better\"? 回答1: To my knowledge they are identical. I think there maybe a few engines out