简体   繁体   English

如何获取元素更改的排序列表的索引?

[英]How can I get index of sorted list where element changes?

I have the following array and I am looking to retrieve the index of the original (sorted) array where the element is changing and how often that individual element exists.我有以下数组,我希望检索元素正在更改的原始(排序)数组的索引以及该单个元素存在的频率。

ab = [1,1,1,3,3,5,5,5,5,5,6,6]

The desired outcome should be like this:想要的结果应该是这样的:

ac = [0,3,5,10]
ad = [3,2,5,2]

Thank you very much for any suggestion.非常感谢您的任何建议。

Cheers.干杯。

You could iterate the array and check the predecessor.您可以迭代数组并检查前任。 If equal, increment the last count, otherwise add the index and a count of one.如果相等,则增加最后一个计数,否则添加索引和计数 1。

 var array = [1, 1, 1, 3, 3, 5, 5, 5, 5, 5, 6, 6], { indices, counts } = array.reduce((r, v, i, a) => { if (a[i - 1] === v) { r.counts[r.counts.length - 1]++; } else { r.indices.push(i); r.counts.push(1); } return r; }, { indices: [], counts: [] }); console.log(...indices); console.log(...counts);

This code produces similar output to the one you posted:此代码产生与您发布的类似的输出:

 var ab = [1,1,1,3,3,5,5,5,5,5,6,6]; var ac = Array.from(new Set(ab.map((e) => ab.indexOf(e)))); var ad = []; for (var i = 0; i < ac.length - 1; i++) { ad.push(ac[i + 1] - ac[i]); } ad.push(ab.length - ac[ac.length - 1]); console.log(...ab); console.log(...ac); console.log(...ad);

Try this, should get you what you want试试这个,应该得到你想要的

        ab = [1,1,1,3,3,5,5,5,5,5,6,6];

        var items = [];
        var positions = [];
        var count = [];

        ab.map((item, index)=>{

            //check if exist
            let item_index = items.indexOf(item);
            if(item_index == -1) {
                items.push(item);
                positions.push(index);
                count.push(1);
            } else {
                let current_count = count[item_index];
                count[item_index] = ++current_count;
            }
        });

        console.log(positions);
        console.log(count);

so, using https://underscorejs.org/#groupBy you can group by value因此,使用https://underscorejs.org/#groupBy您可以按值分组

_.groupBy([1,1,1,3,3,5,5,5,5,5,6,6]);

or 

_.groupBy([1,1,1,3,3,5,5,5,5,5,6,6], function(num){ return num; })

you will get an object like你会得到一个像

{1: [1,1,1], 3: [3,3], 5: [5,5,5,5,5], 6: [6,6]}

so if you take all https://underscorejs.org/#keys and iterate through, value under key is array, take size and append to new array, so you can make ad = [3,2,5,2]因此,如果您获取所有https://underscorejs.org/#keys并遍历,key 下的值是数组,获取大小并附加到新数组,这样您就可以使 ad = [3,2,5,2]

again, iterate through keys and get https://underscorejs.org/#indexOf , you can construct ac = [0,3,5,10]再次,遍历键并获得https://underscorejs.org/#indexOf ,您可以构造 ac = [0,3,5,10]

play around these methods, check examples, and you can do it yourself!玩转这些方法,检查例子,你可以自己做!

I think this works in R. YMMV我认为这适用于 R. YMMV
> ab = c(1,1,1,3,3,5,5,5,5,5,6,6) > ab = c(1,1,1,3,3,5,5,5,5,5,6,6)
> i1<-1:length(ab) > i1<-1:长度(ab)
> i2<-c(2:length(ab),length(ab)) > i2<-c(2:length(ab),length(ab))
> i3<-ab[i1]!=ab[i2] > i3<-ab[i1]!=ab[i2]
> ac<-c(0,i1[i3]) > ac<-c(0,i1[i3])
> ac > 交流
[1] 0 3 5 10 [1] 0 3 5 10
> ad<-c(ac[-1],length(ab))-ac > ad<-c(ac[-1],length(ab))-ac
> ad > 广告
[1] 3 2 5 2 [1] 3 2 5 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM