Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# How to sort a list of strings by frequency?

By : xym2010
Date : July 29 2020, 09:00 AM
code :
from collections import Counter
resl2 = [k for k,v in Counter(resl).items() if v > 500]
resl2 = [(k,v) for k,v in Counter(resl).items() if v > 500]
resl2.sort(key=lambda kv: kv[1])
resl2 = [k for k,v in resl2]

Share :

## How to sort Strings by frequency then by first occurrence

By : Blake Arney
Date : March 29 2020, 07:55 AM
wish helps you Hello maybe this question already answered somewhere else, but I could find it. I have text file which includes , Try this
code :
public static void main(String[] args) throws IOException {

Scanner sc = new Scanner(new File("file.txt"));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()){
}

String[] arr = lines.toArray(new String[0]);
String text = Arrays.toString(arr);
String test = text.replaceAll("\\p{P}","");

List<String> list = Arrays.asList(test.split(" "));
SortedSet<String> uniq = new TreeSet<String>(list);

for (String w : uniq){
System.out.printf("%n%d     %s",Collections.frequency(list, w), w);

}
}

## Frequency Sort Array Of Objects in JS and if frequency Matches sort on basis of object property

Date : March 29 2020, 07:55 AM
like below fixes the issue I have Array of objects , Try this one using reduce and then sort.
Explaination
code :
var arr = [
{"X" : {
"price" : "5"
}
},
{"Y" : {
"price" : "3"
}
},
{"Y" : {
"price" : "3"
}
},
{"Z" : {
"price" : "4"
}
},
{"Q" : {
"price" : "2"
}
},
{"X" : {
"price" : "5"
}
},
{"Z" : {
"price" : "4"
}
},
{"X" : {
"price" : "5"
}
}
];

var frequency = arr.reduce(function (accumulatorObject, currentValue) {
var propName = Object.keys(currentValue)[0];
var elementInArray = accumulatorObject.find((element) => Object.keys(element)[0] === propName);
if (elementInArray) {
elementInArray[propName].count++;
}
else {
var newObject = {};
newObject[propName]  = {};
newObject[propName].count = 1;
newObject[propName].price = +currentValue[propName].price;
accumulatorObject.push(newObject);
}
return accumulatorObject;
}, []); //  // Accumulator starts with Empty array.

frequency.sort(function(first,second){
var diff =  second[Object.keys(second)].count - first[Object.keys(first)].count;
if( diff === 0) {
return second[Object.keys(second)].price - first[Object.keys(first)].price;
}
return diff;});
console.log(frequency);

## Sort a (nested) list of strings on frequency of occurrence

By : BmanUltima
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can't use Counter directly because there are some list mixed in with string, therefore flatten those first:
code :
data = ['Marie', 'Lucas', 'Patsy', 'Julie', 'Peter', 'Emma', 'Kevin', 'Tobias', 'Suzy', ['Marie', 'Lucas'], ['Lucas', 'Marie'], ['Lucas', 'Patsy'], ['Patsy', 'Lucas'], ['Emma', 'Lucas'], ['Lucas', 'Emma'], ['Emma', 'Kevin'], ['Kevin', 'Emma'], ['Peter', 'Emma'], ['Emma', 'Peter'], ['Peter', 'Lucas'], ['Lucas', 'Peter'], ['Peter', 'Julie'], ['Julie', 'Peter'], ['Suzy', 'Tobias'], ['Tobias', 'Suzy']]
from collections import Counter
c = Counter()
for d in data:
if isinstance(d,str):
c[d] += 1
elif isinstance(d,list):
for n in d:
c[n] += 1
c.most_common()
[('Lucas', 9),
('Peter', 7),
('Emma', 7),
('Tobias', 3),
('Patsy', 3),
('Marie', 3),
('Suzy', 3),
('Kevin', 3),
('Julie', 3)]

## Sort a list by frequency of strings in it

By : Sangeetha R
Date : March 29 2020, 07:55 AM
Does that help List is , First, count the occurrence of string and then sort by using Map

## Count frequency of words in a list and sort by frequency

By : onsitesigns
Date : March 29 2020, 07:55 AM
like below fixes the issue I am using Python 3.3 , use this