[英]Using recursion to reverse a list, and count the number of occurances of an element within the list, using python
[英]Finding the number of occurances of a character of each string within a list (python)
我正在寻找一个for循环,该循环查找特定字符的出现次数除以字符串的长度。 之后显示最大的变量
Largestnumber=0
>>>List=["shadow", "light", "blazar"]
>>>Insert= input("input a character(s): ")
for loop
Print (Largestnumber)
它将显示如下:
input a character(s): a#I have inputed an a
0.33333
再次,我正在寻找一种方法来查找字符出现在字符串项中的时间除以确切项的长度的时间。 在我的示例中,“阴影”中包含一个a,长度为6,因此它将为1/6 = 0.1666666。“机翼”中没有任何a,并且其长度为4,因此它将为0/4” blazar“有2个a,整个字符串的长度是6,所以2/6 = 0.3333333,由于0.33333是最大的数字,因此被打印
您将需要五件事:
'<string>'.count('<substring>')
计算<substring>
在<string>
出现的频率。 因此'blazar'.count('a')
将为2。 len(<string>)
将为您提供<string>
的长度。 因此len('blazar')
将为6。 您可以使用简单的列表理解:
Largestnumber=max([float(x.count(Insert))/len(x) for x in List])
@ Galax指出的EDIT ,列表理解可以省略
Largestnumber=max(float(x.count(Insert))/len(x) for x in List)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.