Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的字符计数_Python_Sorting_Count_Charactercount - Fatal编程技术网

Python中的字符计数

Python中的字符计数,python,sorting,count,charactercount,Python,Sorting,Count,Charactercount,任务已给出:需要从用户处获取一个单词,则单词中的字符总数必须计数并按排序顺序显示计数必须是降序,字符必须是升序- 即。, 如果用户提供as管理 那么输出应该是 **a 2 e 2 m 2 n 2 g 1 t 1** 这是我为该任务编写的代码: string=input().strip() set1=set(string) lis=[] for i in set1: lis.append(i) lis.sort() while len(lis)>0: maxi=0 for i in

任务已给出:需要从用户处获取一个单词,则单词中的字符总数必须计数并按排序顺序显示计数必须是降序,字符必须是升序- 即。, 如果用户提供as管理 那么输出应该是

**a 2
e 2
m 2
n 2
g 1
t 1**
这是我为该任务编写的代码:

string=input().strip()
set1=set(string)
lis=[]
for i in set1:
 lis.append(i)
lis.sort()
while len(lis)>0:
 maxi=0
 for i in lis:
  if string.count(i)>maxi:
   maxi=string.count(i)
 for j in lis:
  if string.count(j)==maxi:
   print(j,maxi)
   lis.remove(j)
这段代码为字符串管理提供了以下输出

a 2
m 2
e 2
n 2
g 1
t 1
m&e未分类。 我的代码有什么问题?

您可以使用简单的


我不确定通过添加while循环和两个嵌套for循环来实现什么。但同样的事情也可以通过一个for循环来实现

对于lis中的i: printi,string.counti 这样,输出将是:

a 2
e 2
g 1
m 2
n 2
t 1

代码的问题是变量maxi和两个for循环的赋值。e不会排在第二位,因为您将maxi指定为2,并且string.counti将小于maxi

 for i in lis:
  if string.count(i)>maxi:
   maxi=string.count(i)

 for j in lis:
  if string.count(j)==maxi:
   print(j,maxi)

有几种方法可以实现你想要的目标。您可以尝试其他人解释的解决方案。

如前所述,您可以使用计数器获取字符数,无需设置或列出

对于排序,您最好使用内置的sorted函数,该函数接受key参数中的函数。阅读更多关于和的信息

>>>从收款进口柜台 >>>c=柜台“管理” >>>分类DC项目 [a',2',e',2',g',1',m',2',n',2',t',1] >>>alpha_sorted=sorteddc.items >>>SorterDalpha_排序,键=λx:x[1] [g',1',t',1',a',2',e',2',m',2',n',2] >>>sortedalpha_排序,key=lambda x:x[1],reverse=True reverse确保您得到降序排序 [a',2',e',2',m',2',n',2',g',1',t',1]
代码的问题在于,您试图从列表中删除一个元素,而您仍在对其进行迭代。现在,你移除了a,于是e占据了它的位置——列表前进到下一个字母m。因此,e被跳过“直到下一次迭代”

尝试将打印和删除分开,不要从当前迭代的列表中删除元素,而是尝试将所有其他元素添加到新列表中

string=input.strip set1=setstring lis=[] 对于set1中的i: 阑尾李 李斯特 当lenlis>0时: 最大值=0 对于lis中的i: 如果string.counti>maxi: maxi=string.counti 对于lis中的j: 如果string.countj==maxi: printj,maxi 杜佩利斯=利斯 lis=[] 对于dupelis中的k: 如果string.countk=马克西: 附属物 管理A 2e 2m 2n 2g 1t 1


计数字符的最简单方法是使用计数器,正如前面的一些答案所建议的那样。在这之后,诀窍是想出一个同时考虑计数和字符的方法来实现排序。我有以下资料:

from collections import Counter

c = Counter('management')

sc = sorted(c.items(),
            key=lambda x: -1000 * x[1] + ord(x[0]))

for char, count in sc:
    print(char, count)
c、 items提供元组列表字符、计数。我们可以使用sorted对它们进行排序

参数键就是键。排序将具有较低键的项(即具有较小值的键)放在第一位,因此我必须对具有较小值的项进行较大计数

我基本上给计数x[1]赋予了很多负权重-1000,然后用字符ordx[0]的ascii值来增加它。结果是一个排序顺序,首先考虑计数,其次考虑字符


一个基本假设是ordx[0]永远不会超过1000,这对于英语字符应该是正确的。

这怎么可能是一个答案?你忽略了他们正在做的事情,让事情变得更糟,删除了至少部分有效的部分…谢谢大家的回答。祝你度过愉快的一天。
 for i in lis:
  if string.count(i)>maxi:
   maxi=string.count(i)

 for j in lis:
  if string.count(j)==maxi:
   print(j,maxi)
from collections import Counter

c = Counter('management')

sc = sorted(c.items(),
            key=lambda x: -1000 * x[1] + ord(x[0]))

for char, count in sc:
    print(char, count)