我尝试匹配/获取字符串中的所有重复项。这是我到目前为止所做的:
var str = 'abcabc123123';
var REPEATED_CHARS_REGEX = /(.).*\1/gi;
console.log( str.match(REPEATED_CHARS_REGEX) ); // => ['abca', '1231']
Run Code Online (Sandbox Code Playgroud)
如您所见,匹配结果为['abca', '1231'],但我希望得到['abc', '123']. 有什么想法可以实现吗?
第二个问题:
我期望的另一件事是可以更改字符需要在字符串中多久才能匹配的持续时间...
例如,如果字符串是abcabcabc并且重复时间设置为2它应该导致['abcabc']. 如果设置为3它应该是['abc'].
更新
一个非RegExp解决方案是完全可以的!
我需要某种功能或小技巧来解决我的问题。
所以我得到了一个列表,
[1,2,3,4]
但我需要这个数组更长,重复相同的元素,所以假设我需要一个长度为 10 的数组,所以它变成:
[1,2,3,4,1,2,3,4,1,2]
所以我需要以相同的顺序使用与列表中相同的值来扩展列表
returnString = the array or string to return with extended elements
array = the basic array which needs to be extended
length = desired length
Run Code Online (Sandbox Code Playgroud)
编辑:
returnString = ""
array = list(array)
index = 0
while len(str(array)) != length:
if index <= length:
returnString += array[index]
index += 1
else:
toPut = index % length
returnString.append(array[toPut])
index += 1
return returnString
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个需要 3 个输入的函数:一个字符串(已命名word)、一个整数(已命名n)、另一个字符串(已命名delim',然后该函数必须重复命名word n时间的字符串(这很容易),并且在每次重复之间它都有插入名为 的字符串delim。
我知道这段代码有效:
print('me', 'cat', 'table', sep='&')
Run Code Online (Sandbox Code Playgroud)
但这段代码没有:
print(cat*3, sep='&')
Run Code Online (Sandbox Code Playgroud)
我写的代码几乎没用,但我还是会发布它——可能还有我不知道的其他错误或不准确之处。
def repeat(word, n, delim):
print(word*n , sep=delim)
def main():
string=input('insert a string: ')
n=int(input('insert number of repetition: '))
delim=input('insert the separator: ')
repeat(string, n, delim)
main()
Run Code Online (Sandbox Code Playgroud)
例如,给定以下输入:
word='cat', n=3, delim='petting'
Run Code Online (Sandbox Code Playgroud)
我希望该程序回馈:
catpettingcatpettingcat
Run Code Online (Sandbox Code Playgroud) 我想用 n 次传递一个字符串以用空格重复它,我的代码如下所示:
def repeating(word, n):
return word * n
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像:
hello hello hello
Run Code Online (Sandbox Code Playgroud)
每个字符串之间有空格。
我需要一个接受所有类型字符(字母,数字和所有特殊字符)的正则表达式,并且miniumum字符数应为15,并且对最大字符没有限制.
我在python中有一个函数需要不断运行并检查一些变量.目前我有一个while循环,一切都顺利运行,直到我想使用pyqt小部件,最终导致灾难.
我已经在stackoverflow中的其他线程上找到了twisted.internet模块,但我不能让它在python3上运行.
是否有另一个模块允许我重复某个函数在python3中每0.01秒运行一次而不会闲置我的代码?
也许这之前被问过,在这种情况下我会删除这个问题,但我有两个列表:
occurence_list = [1, 2, 3, 4, 5]
value_list = [10, 20, 30, 40, 50]
Run Code Online (Sandbox Code Playgroud)
我希望每个值看起来与另一个列表中相同索引的值相同:
result = [10, 20, 20, 30, 30, 30, 40, 40, 40, 40, 50, 50, 50, 50, 50]
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?
我正在使用delphi7.我想在我的程序中放一首歌,但我不希望它永远不会结束.我尝试使用计时器,但它没有播放音乐:
procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var playsound,destination:string;
begin
destination:=paramstr(0);
playsound:=extractfilepath(destination)+'Soundtrack\play.wma';
mediaplayer1.FileName:=playsound;
mediaplayer1.Open;
mediaplayer1.Play; //USING TMEDIAPLAYER
end;
Run Code Online (Sandbox Code Playgroud)
此代码中没有语法错误,但是歌曲没有运行,也许计时器不适用于该作业.我该怎么办?谢谢
我在 R 中有这个代码
a <- c(NA)
a<- matrix(c(a), nrow = 80)
a<-as.data.frame(a)
a[ c(T,F,F,F), ] <- "aaa"
a[ c(F,T,F,F), ] <- "bbb"
a[ c(F,F,T,F), ] <- "ccc"
a[ c(F,F,F,T), ] <- "ddd"
Run Code Online (Sandbox Code Playgroud)
如何在 python 中将它复制为熊猫数据框?
str(a)
Run Code Online (Sandbox Code Playgroud)
'data.frame':80 个观察。共 1 个变量:
$ V1: chr "aaa" "bbb" "ccc" "ddd" ...
repeat ×9
python ×6
string ×3
python-3.x ×2
regex ×2
arrays ×1
asp.net ×1
audio ×1
character ×1
dataframe ×1
delphi ×1
javascript ×1
list ×1
media-player ×1
pandas ×1