I have
list1 = ["one", "two", "two", "three", "five", "five", "five", "six"]
and the output should be
list2 = ["five" , "two", "one", "three" , "six"]
"five"
is the first element because in list1 has the highest number of occurrences (3)"two
" is the second element because in list1 has the next highest number of occurrences (2)"one"
, "three
" and "six"
have the same lower number of occurrences (1) so they are last in my list2
- it doesn't really matter what order as long as they are after "five" and "two". list2 = ["five" , "two", "six", "three", "one"]
or list2 = ["five" , "two", "three", "one", "six"]
or any other variations are acceptable.You could use a list comprehension and Counter
:
from collections import Counter
print([element for element,count in Counter(list1).most_common()])
Outputs:
['five', 'two', 'three', 'six', 'four', 'one']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With