Python按频率对字符串排序-无法使用sorted()函数排序 [英] Python Sort string by frequency - cannot sort with sorted() function

查看:63
本文介绍了Python按频率对字符串排序-无法使用sorted()函数排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对按频率对简单字符串进行排序有一个问题(我将一个字符串作为输入,并且需要按降序将一个排序后的字符串作为输出返回).让我举一个例子(原始单词包含4个e,2个s,1个t,1个r和1个d;因此将它们排序):

I have an issue with sorting a simple string by frequency (I get a string as an input, and I need to give a sorted string back as an output in descending order). Let me give you an example (the original word contains 4 e's, 2 s's, 1 t, 1 r and 1 d; so these get sorted):

In [1]: frequency_sort("treeseeds")
Out [1]: "eeeesstrd"

大多数关于Stack Overflow的解决方案都指出,我应该使用 sorted()函数来获取结果,但是,它似乎仅在某些情况下有效.

Most solutions on Stack Overflow state that I should use the sorted() function to get my results, however, it only seems to work with certain cases.

我做了两个应该起作用的功能,但是似乎没有一个功能可以用我的特定输入(见下文)完成.

I made two functions that supposed to work, but none of them seems to do the trick with my specific inputs (see below).

第一个功能:

def frequency_sort(s):
    s_sorted = sorted(s, key=s.count, reverse=True)
    s_sorted = ''.join(c for c in s_sorted)
    return s_sorted

第二功能:

import collections
def frequency_sort_with_counter(s):
    counter = collections.Counter(s)
    s_sorted = sorted(s, key=counter.get, reverse=True)
    s_sorted = ''.join(c for c in s_sorted)
    return s_sorted

使用这两个功能,我的输出看起来像这样:

With both functions my outputs look like this:

第一个输出正常:

In [1]: frequency_sort("loveleee")
Out [1]: "eeeellov"

第二个输出不是很多

In [2]: frequency_sort("loveleel")
Out [2]: "leleelov"

第三个输出完全混乱:

In [3]: frequency_sort("oloveleelo")
Out [3]: "oloeleelov"

可能出了什么问题?它是否以某种方式连接到"o"和"l"字符?还是我只是想念什么?

What could have gone wrong? Is it connected to the 'o' and 'l' characters somehow? Or am I just missing something?

推荐答案

在多个字符具有相同频率的字符串中,您提出的算法无法区分出现次数相同的字符.您可以通过使用频率和字符本身的元组进行排序来解决此问题;例如

In a string where multiple characters have the same frequency, the algorithms you proposed have no way of distinguishing between characters that appear the same number of times. You could address this by sorting using a tuple of the frequency and the character itself; e.g.

In [7]: def frequency_sort(s):
        s_sorted = sorted(s, key=lambda c: (s.count(c), c), reverse=True)
        s_sorted = ''.join(c for c in s_sorted)
        return s_sorted
   ...: 

In [8]: frequency_sort("loveleel")
Out[8]: 'llleeevo'

这篇关于Python按频率对字符串排序-无法使用sorted()函数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆