[英]counting the number of consecutive repetitions
I have a list of elements:我有一个元素列表:
list = ['green','green','red','blue','red','blue','yellow','white','black','yellow','white','black','red','green','yellow','black']
I want to group the consecutive repetitions, no matter what size they are.我想对连续的重复进行分组,无论它们的大小。 meaning that this is my desired output.
这意味着这是我想要的 output。
['green x2','red-blue-x2','yellow-white-black x2','red,'green','yellow','black']
I was able to achieve only a part of it with js , here's the code:我只能用js实现它的一部分,这是代码:
let sentence = ""
let index = 0
let repetition_count = 1
let repetition_list = []
list.forEach(element => {
let prev_elem = list[index-1]
if(element == prev_elem)
{
repetition_count++
repetition_list.push(element)
}
else if(repetition_count>1){
if(sentence.endsWith(repetition_list[0]+"-")){
sentence = remove_last_word(sentence)
}
sentence+= repetition_list[0] + "x"+repetition_count+"-"
repetition_count = 1
repetition_list = []
}
sentence+= element+"-"
index++;
}
with some tweaks to the above code it produced the following output:通过对上述代码进行一些调整,它产生了以下 output:
['green x2','red','blue','red','blue','yellow','white','black','yellow','white','black','red,'green','yellow','black']
now I don't know how to check more than one element for repetition.现在我不知道如何检查多个元素是否重复。
I tried list comprehension with groupby
(itertools) in python but it didn't do much more than the JS code.我在 python 中尝试使用
groupby
(itertools) 进行列表理解,但它并没有比JS代码做得更多。
PYTHON CODE : PYTHON 代码:
grouped_sentence = [(k, sum(1 for i in g)) for k, g in groupby(list)]
and it gave me this output:它给了我这个 output:
[ ('green',2),('red',1),('blue',1),('red',1),('blue',1),('yellow',1),('white',1),('black',1),('yellow',1),('white',1),('black',1),('red',1),('green',1),('yellow',1),('black',1)]
any suggestions?有什么建议么?
First, this was an interesting mental exercise, so thank you.首先,这是一个有趣的心理练习,所以谢谢。
This function will check an array for repeats up to a maximum grouping size.此 function 将检查数组的重复次数,直至达到最大分组大小。
function getRepeats(list, maxgroupingsize) {
var temparray = [...list];
// iterate through each grouping size
for (var grpcounter = maxgroupingsize; grpcounter > 0; --grpcounter) {
// iterate through the array
for (var pos = 0; pos < temparray.length - grpcounter + 1; ++pos) {
if (temparray[pos] == "") {continue;}
// create a current selection string
var myselection = "";
for (var selectionbuild = 0; selectionbuild < grpcounter; ++selectionbuild) {
if (myselection != "") { myselection += "-"; }
myselection += temparray[pos + selectionbuild]
}
var matches = 0;
// iterate through the array forward looking for matches
for (var checkpos = pos + grpcounter; checkpos < temparray.length - grpcounter + 1; ++checkpos) {
// create a test selection string
var testselection = "";
for (var selectionbuild = 0; selectionbuild < grpcounter; ++selectionbuild) {
if (testselection != "") { testselection += "-"; }
testselection += temparray[checkpos + selectionbuild];
}
// test to see if the selections match
if (testselection === myselection) {
++matches;
// empty out the array values that we matched
for (var cleancounter = 0; cleancounter < grpcounter; ++cleancounter) {
temparray[checkpos + cleancounter] = "";
}
checkpos += grpcounter - 1;
} else {
break;
}
}
// if we had matches, update the current array value
if (matches > 0) {
myselection += " x" + (matches + 1);
checkpos += ((matches + 1) * grpcounter)
temparray[pos] = myselection;
for (var cleancounter = 1; cleancounter < grpcounter; ++cleancounter) {
temparray[pos + cleancounter] = "";
}
}
}
}
// create a new array without empty values
var myresult = [];
for (var counter = 0; counter < temparray.length; ++counter) {
if (temparray[counter] != "") { myresult.push(temparray[counter]); }
}
// return the result
return myresult;
}
And you would call it like this:你会这样称呼它:
var result = getRepeats(mylist, 3);
Where mylist is your array and 3 means it will check for groups up to 3 elements long.其中 mylist 是您的数组,3 表示它将检查最多包含 3 个元素的组。
The following array:以下数组:
[ "red", "orange", "orange", "orange", "yellow", "green", "blue", "green", "blue",
"green", "blue", "orange", "orange", "red", "yellow", "yellow", "indigo", "violet",
"red", "green", "violet", "red", "green", "violet", "red", "green", "violet", "orange",
"red", "indigo", "indigo", "indigo", "orange" ]
Returns:回报:
[ "red", "orange x3", "yellow", "green-blue x3", "orange x2", "red", "yellow x2",
"indigo", "violet-red-green x3", "violet", "orange", "red", "indigo x3", "orange" ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.