Python统计list中各个元素出现的次数

您所在的位置:网站首页 python用字典输出每个字符出现的次数 Python统计list中各个元素出现的次数

Python统计list中各个元素出现的次数

2024-07-13 11:22| 来源: 网络整理| 查看: 265

列表count()函数调用方法

对象.count(参数)

count()方法操作示例

有列表['a','iplaypython.com','c','b‘,'a'],想统计字符串'a'在列表中出现的次数,可以这样操作

>>> ['a','iplaypython.com','c','b','a'].count('a')

2

其返回值就是要统计参数出现的次数。在应用的时候最好是把列表赋给一个变量,之后再用count()方法来操作比较好。

当对象是一个嵌套的列表时,要查找嵌套列表中的列表参数count()方法同样可以完成

>>> x = [1,2,'a',[1,2],[1,2]]

>>> x.count([1,2])

2

>>> x.count(1)

1

>>> x.count('a')

1

 

 

1. 计算字母和数字出现的次数 str='abc123abc456aa'd={}for x in str:    print x    if not x in d:        d[x]=1    else:        d[x]=d[x]+1 print d {'a': 4, 'c': 2, 'b': 2, '1': 1, '3': 1, '2': 1, '5': 1, '4': 1, '6': 1}

#!/usr/bin/python3

str="ABCdefabcdefabc"str=str.lower()str_list=list(str)char_dict={}

for char1 in str:      if char1 in char_dict:          count=char_dict[char1]      else:         count=0     count=count+1     char_dict[char1]=count print(char_dict)

a = "aAsmr3idd4bgs7Dlsf9eAF"

请将a字符串的数字取出,并输出成一个新的字符串。

请统计a字符串出现的每个字母的出现次数(忽略大小写,a与A是同一个字母),并输出成一个字典。 例 {'a':3,'b':1}

请去除a字符串多次出现的字母,仅留最先出现的一个,大小写不敏感。例 'aAsmr3idd4bgs7Dlsf9eAF',经过去除后,输出 'asmr3id4bg7lf9e'

a = "aAsmr3idd4bgs7Dlsf9eAF"

def fun1_2(x): #1&2 x = x.lower() #大小写转换 num = [] dic = {} for i in x: if i.isdigit(): #判断如果为数字,请将a字符串的数字取出,并输出一个新的字符串 num.append(i) else: #2 请统计a字符串出现每个字母的出现次数(忽视大小写),并输出一个字典。例:{'a':3,'b':1} if i in dic: continue else: dic[i] = x.count(i) new = ''.join(num) print "the new numbers string is: " + new print "the dictionary is: %s" % dic fun1_2(a)

 

 

def fun3(x): x = x.lower() new3 = [] for i in x: if i in new3: continue else: new3.append(i) print ''.join(new3) fun3(a)

 

三种方法:①直接使用dict

②使用defaultdict

③使用Counter

ps:`int()`函数默认返回0

①dict1. text = "I'm a hand some boy!"2.  3. frequency = {}4.  5. for word in text.split():6.    if word not in frequency:7.        frequency[word] = 18.    else:9.        frequency[word] += 1

②defaultdict1. import collections2.  3. frequency = collections.defaultdict(int)4.  5. text = "I'm a hand some boy!"6.  7. for word in text.split():8.    frequency[word] += 1

③Counter1. import collections2.  3. text = "I'm a hand some boy!"4. frequency = collections.Counter(text.split())

现有列表如下:[6, 7, 5, 9, 4, 1, 8, 6, 2, 9]希望统计各个元素出现的次数,可以看作一个词频统计的问题。我们希望最终得到一个这样的结果:{6:2, 7:1...}即 {某个元素:出现的次数...}首先要将这些元素作为字典的键,建立一个初值为空的字典:

>>> from random import randint

>>> l = [randint(1,10) for x in xrange(10)]

>>> l[6, 7, 5, 9, 4, 1, 8, 6, 2, 9]

>>> d = dict.fromkeys(l, 0)>>> d{1: 0, 2: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}

# 现在的任务是需要将d中每个键所对应的值统计出来>>> for x in l:>>>     d[x] += 1>>> d

{1: 1, 2: 1, 4: 1, 5: 1, 6: 2, 7: 1, 8: 1, 9: 2}

# 这就统计完了所有的元素出现的次数

另外一种方法,利用collections模块中的Counter对象

>>> from collections import Counter

# 这个Counter可以直接接受一个列表,将它转化为统计完成的结果

>>> d = Counter(l)>>> dCounter({6: 2, 9: 2, 1: 1, 2: 1, 4: 1, 5: 1, 7: 1, 8: 1})# 该Counter对象是字典对象的子类,也可以通过键来访问对应值>>> d[6]2# Counter对象方便之处在于它内置有most_common(n)方法,可以直接统计出前n个最高词频>>> d.most_common(2)[(6, 2), (9, 2)]

用python做词频统计

import stringimport time

path='C:\\Users\\ZHANGSHUAILING\\Desktop\\Walden.txt'

with open(path,'r') as text:    words=[raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]    words_index=set(words)    counts_dict={index:words.count(index) for index in words_index}for word in sorted(counts_dict,key=lambda x:counts_dict[x],reverse=True):    time.sleep(2)    print ('{}--{} times'.format(word,counts_dict[word]))

{'the': 2154, 'and': 1394, 'to': 1080, 'of': 871, 'a': 861, 'his': 639, 'The': 637, 'in': 515, 'he': 461, 'with': 310, 'that': 308, 'you': 295, 'for': 280, 'A': 269, 'was': 258, 'him': 246, 'I': 234, 'had': 220, 'as': 217, 'not': 215, 'by': 196, 'on': 189, 'it': 178, 'be': 164, 'at': 153, 'from': 149, 'they': 149, 'but': 149, 'is': 144, 'her': 144, 'their': 143, 'who': 131, 'all': 121, 'one': 119, 'which': 119,}#部分结果展示

import re,collectionsdef get_words(file):    with open (file) as f:        words_box=[]        for line in f:                                     if re.match(r'[a-zA-Z0-9]*',line):#避免中文影响                words_box.extend(line.strip().split())                   return collections.Counter(words_box)print(get_nums('emma.txt')+get_nums('伊索寓言.txt'))

import re,collectionsdef get_words(file):    with open (file) as f:        words_box=[]        for line in f:                                         if re.match(r'[a-zA-Z0-9]',line):                    words_box.extend(line.strip().split())                   return collections.Counter(words_box)a=get_nums('emma.txt')+get_nums('伊索寓言.txt')print(a.most_common(10))

python 计数方法小结

方法一: 遍历法

def get_counts(sequence):    counts = {}    for x in sequence:        if x in counts:            counts[x] += 1        else:            counts[x] = 1    return counts这是最常规的方法,一个一个数咯

方法二: defaultdict

这里用到了coollections 库

from collections import defaultdict

def get_counts2(sequence):    counts = defaultdict(int) #所有值被初始化为0    for x in sequence:        counts[x] += 1    return counts最后得到的是 元素:个数 的一个字典

方法三:value_counts()

这个方法是pandas 中的 ,所以使用时候需要先导入pandas ,该方法会对元素计数,并按从大到小的顺序排列

tz_counts = frame['tz'].value_counts()tz_counts[:10]

>>>America/New_York       1251                        521America/Chicago         400America/Los_Angeles     382America/Denver          191Europe/London            74Asia/Tokyo               37Pacific/Honolulu         36Europe/Madrid            35America/Sao_Paulo        33Name: tz, dtype: int64我们看一下官方文档中的说明

Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)[source]?Returns object containing counts of unique values.12这里说明一下返回的数据是Series 格式的

总的来说 方法一最为普通 如果数据量比较大的话 非常费时间 ,方法三对数据的格式有要求 ,所以推荐使用方法二

python - 统计一个字符串中的每一个字符出现了多少次(先将字符串转换为列表再统计)

#coding=utf-8

#统计一个字符串中的每一个字符出现了多少次

#定义一个字符串str = 'abbcccdddd'

#在字符串的每一个字符之间插入一个空格组成一个新的字符串

str = ' '.join(str)

#打印新的字符串看看

print('str = ',str)

#将新字符串按空格分割成一个列表

li = str.split(' ')

#打印新的列表

print('li = ',li)

#统计每一个字符出现的次数:

#方式一

for i in set(li):    if li.count(i) >= 1:        print('%s 出现了%d 次!'%(i, li.count(i)))

print('*'*50)

#方式二

from collections import Counterres = Counter(li)print(res)

运行结果:

('str = ', 'a b b c c c d d d d')('li = ', ['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd'])a 出现了1 次!c 出现了3 次!b 出现了2 次!d 出现了4 次!**************************************************Counter({'d': 4, 'c': 3, 'b': 2, 'a': 1})

Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

count()方法语法:

str.count(sub, start= 0,end=len(string))

参数sub -- 搜索的子字符串start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

返回值

该方法返回子字符串在字符串中出现的次数。

#!/usr/bin/python str = "this is string example....wow!!!"; sub = "i";

print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)

sub = "wow";

print "str.count(sub) : ", str.count(sub)

#定义统计字符串的方法

def calcu_sub_str_num(mom_str,sun_str):  print('打印母字符串:',mom_str)   #打印出母字符串  print( '打印子字符串:',sun_str)  #打印出子字符串  print('打印母字符串长度:',len(mom_str)) #打印出母字符串长度  print( '打印子字符串长度:',len(sun_str))  #打印出子字符串长度  count = 0                                  #定义计数器初始值  #使用循环遍历字符串,第一次循环,通过切片获取下标从0开始与子字符串长度一致的字符串,并与字符串比较,如果等于子字符串count+1  #第二次循环,通过切片获取下标从1开始与子字符串长度一致的字符串,并与字符串比较,如果等于子字符串则count+1,以此类推直到遍历完成  for i in range(len(mom_str)-1): #因为i的下标从0开始,所以len(mom_str)-1      if mom_str[i:i+len(sun_str)] == sun_str:          count+=1  return count

mom_str = input('please input mother string:') #使用input获取输入母字符串sun_str = input('please input child string:') #使用input获取输入子字符串print('子字符串在母字符串中出现的次数:%d'%calcu_sub_str_num(mom_str,sun_str))#%d为数字占位符

 

例8:使用zip方法构建元素为元组的列表

In [91]: zip('xyz','123')

Out[91]: [('x', '1'), ('y', '2'), ('z','3')]

In [92]: zip('xyz','1234')

Out[92]: [('x', '1'), ('y', '2'), ('z','3')]

In [93]: zip('xyzm','564')

Out[93]: [('x', '5'), ('y', '6'), ('z','4')]

In [94]: zip('xyz','123','abc')

Out[94]: [('x', '1', 'a'), ('y', '2', 'b'),('z', '3', 'c')]

例9:使用dict(zip())快速构建字典

In [95]: dict(zip('xyz','123'))

Out[95]: {'x': '1', 'y': '2', 'z': '3'}--------------------- 

1) 使用字典dict()

循环遍历出一个可迭代对象中的元素,如果字典没有该元素,那么就让该元素作为字典的键,并将该键赋值为1,如果存在就将该元素对应的值加1.

lists = ['a','a','b',5,6,7,5]        count_dict = dict()        for item in lists:            if item in count_dict:                count_dict[item] += 1            else:                count_dict[item] = 1

2) 使用defaultdict()

defaultdict(parameter)可以接受一个类型参数,如str,int等,但传递进来的类型参数,不是用来约束值的类型,更不是约束键的类型,而是当键不存在的话,实现一种值的初始化

defaultdict(int):初始化为 0defaultdict(float):初始化为 0.0defaultdict(str):初始化为 ”from collections import defaultdict        lists = ['a', 'a', 'b', 5, 6, 7, 5]        count_dict = defaultdict(int)        for item in lists:            count_dict[item] += 1

3)使用集合(set)和列表(list)

先使用set去重,然后循环的把每一个元素和每一个元素对应的次数lists.count(item)组成一个元组放在列表里面

lists = ['a', 'a', 'b', 5, 6, 7, 5]count_set = set(lists)count_list = list()for item in count_set:      count_list.append((item,lists.count(item)))

4)使用Counter

Counter是一个容器对象,主要的作用是用来统计散列对象,可以使用三种方式来初始化

参数里面参数可迭代对象 Counter("success")

传入关键字参数Counter((s=3,c=2,e=1,u=1))

传入字典 Counter({"s":3,"c"=2,"e"=1,"u"=1})

Counter()对象还有几个可以调用的方法,代码里面分别进行了说明

from collections import Counterlists = ['a', 'a', 'b', 5, 6, 7, 5]a = Counter(lists)print(a)  # Counter({'a': 2, 5: 2, 'b': 1, 6: 1, 7: 1})

a.elements() # 获取a中所有的键,返回的是一个对象,我们可以通过list来转化它

a.most_common(2) # 前两个出现频率最高的元素已经他们的次数,返回的是列表里面嵌套元组

a['zz']  # 访问不存在的时候,默认返回0

a.update("aa5bzz") # 更新被统计的对象,即原有的计数值与新增的相加,而不是替换

a.subtrct("aaa5z") # 实现与原有的计数值相减,结果运行为0和负值

 

 

 

 

利用Python字典统计 利用Python的collection包下Counter类统计 利用Python的pandas包下的value_counts类统计

 

字典统计

a = [1, 2, 3, 1, 1, 2] dict = {} for key in a: dict[key] = dict.get(key, 0) + 1 print(dict)

 

 

collection包下Counter类统计

 

from collections import Counter a = [1, 2, 3, 1, 1, 2] result = Counter(a) print(result)

 

pandas包下的value_counts方法统计

import pandas as pd a = pd.DataFrame([[1,2,3], [3,1,3], [1,2,1]]) result = a.apply(pd.value_counts) print(result)

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3