Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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_Python 3.x - Fatal编程技术网

Python—计算列表中字符串的频率

Python—计算列表中字符串的频率,python,python-3.x,Python,Python 3.x,我想知道如何计算列表中字符串的频率并使用它们 example list: fruits = (['apple', 'banana', 'banana', 'apple', 'mango']) def fruitcounter(): from collections import Counter a = Counter(fruits) return(a) Counter({'apple': 2, 'banana': 2, 'mango': 1}) 水果清单是随机的。 有没

我想知道如何计算列表中字符串的频率并使用它们

example list: fruits = (['apple', 'banana', 'banana', 'apple', 'mango'])
def fruitcounter():
    from collections import Counter
    a = Counter(fruits)
    return(a)
Counter({'apple': 2, 'banana': 2, 'mango': 1})
水果清单是随机的。
有没有办法使用这些数字并分配它们,以便我可以进行其他计算,如列表中苹果的百分比?

尝试在不使用计数器库的情况下执行此操作

fruit=['apple', 'banana', 'banana', 'apple', 'mango']
unique_words = set(fruit) 
c=[]  
countvalue={}
allpercent={}
for key in unique_words: 
        c= fruit.count(key)     #count no of fruits
        countvalue[key]=c   
        percent=c/len(fruit)*100  #calculate percent
        allpercent[key] = percent
        print('percent of ',key,' is ' , percent)
print('\ncounts no of fruits in dictionary \n')  
print(countvalue)

print('\npercentage value in dictionary \n')
print(allpercent)
#output
##percent of  mango  is  20.0
#percent of  banana  is  40.0
#percent of  apple  is  40.0

#counts no of fruits in dictionary 

#{'mango': 1, 'banana': 2, 'apple': 2}

#percentage value in dictionary 

#{'mango': 20.0, 'banana': 40.0, 'apple': 40.0}

要计算列表中苹果的百分比,您可以访问苹果的频率并除以水果的总量,如下所示:

from collections import Counter
fruits = ['apple', 'banana', 'banana', 'apple', 'mango']

c = Counter({'apple': 2, 'banana': 2, 'mango': 1})
apple_percentage = 100 * c['apple']/len(fruits)
print(f'{apple_percentage}%')
输出:

40.0%

您可以使用ether user
len(fruits)
或对计数器值求和,以获得水果总量:

from collections import Counter

fruits = (['apple', 'banana', 'banana', 'apple'])
counts = Counter(fruits)
totals = sum(counts.values())

bananaPercent = 100/totals*counts['banana']
print('You got %s percent bananas!' % bananaPercent)
输出:

You got 50 percent bananas!
apples make 50 percent!
bananas make 50 percent!
如果您不知道水果,请编辑:

from collections import Counter

fruits = (['apple', 'banana', 'banana', 'apple'])
counts = Counter(fruits)
totals = sum(counts.values())

for k in counts.keys():
    kPercent = 100/totals*counts[k]
    print('%ss make %s percent!' % (k, kPercent))
输出:

You got 50 percent bananas!
apples make 50 percent!
bananas make 50 percent!

使用字典理解,将所有计数除以列表长度

a = Counter(fruits)
percents = {fruit: count/len(fruits)*100 for fruit, count in a.items()}
结果:

{'apple': 40, 'banana': 40, 'mango': 20}

counts=Counter(fruits)
然后使用
counts['mango']
counts['apple']
?您可以像对待任何
dict
对象一样对待
Counter
对象,也就是说,如果您将其分配给变量:
c=fruitcounter()
您现在可以使用
c['apple']
等等。您有频率和列表大小,为什么不能计算百分比?谢谢您的快速评论。水果清单不详,这只是一个例子。列表中可能有5个字符串,可以是任意数字,也可以有不同的水果,比如菠萝。响应@Jarmod,我不能使用counts['mango']或counts['apple'],因为我可能不知道列表中会有什么水果。OP是否说他想分配给计数器?似乎更像是一个关于如何访问频率的问题。为什么
len(fruits)
不同于
sum(counts.values())
?我想
len(…)
,它会更快:)@Maurice Meyer。对于输出,有没有办法在同一行中得到“苹果赚50%,香蕉赚50%?在打印调用结束时告诉打印函数您想要什么,这将实现类似于您正在寻找的东西<代码>打印('%ss make%s percent!'%(k,kPercent),end='')这将在输出的末尾用空格替换默认换行符。