counterpython
❶ python中类的实例对象的理解总结
9.3.3. 实例对象
现在我们可以用实例对象作什么?实例对象唯一可用的操作就是属性引用。有两种有效的属性名。
数据属性 相当于 Smalltalk 中的“实例变量”或 C++ 中的“数据成员”。和局部变量一样,数据属性不需要声明,第一次使用时它们就会生成。例如,如果 x 是前面创建的 MyClass 实例,下面这段代码会打印出 16 而在堆栈中留下多余的东西:
x.counter = 1
while x.counter < 10:
x.counter = x.counter * 2
print(x.counter)
del x.counter
另一种为实例对象所接受的引用属性是 方法。方法是“属于”一个对象的函数。(在 Python 中,方法不止是类实例所独有:其它类型的对象也可有方法。例如,链表对象有 append,insert,remove,sort 等等方法。然而,在后面的介绍中,除非特别说明,我们提到的方法特指类方法)
实例对象的有效名称依赖于它的类。按照定义,类中所有(用户定义)的函数对象对应它的实例中的方法。所以在我们的例子中,x.f 是一个有效的方法引用,因为 MyClass.f 是一个函数。但 x.i 不是,因为 MyClass.i 不是函数。不过 x.f 和 MyClass.f 不同,它是一个 方法对象 ,不是一个函数对象。
❷ Python 统计列表里面有多少个元素
Python 统计列表里面有多少个元素步骤如下:
1、打开python语言命令窗口,定义一个列表变量Z并打印对应的列表值。
❸ python collections counter的值怎么取出来
使用counter类的items()函数即可,Python3程序如下:
foreachincount.items():#假设存在counter类的count
(a,b)=each
print(a,b.sep=' ',end=' ',file='./test.txt')
❹ python counter类的keys怎么排序
(1)从Collections集合模块中引入集合类Counter
(2)Counter(a)可以打印出数组a中每个元素出现的次数
(3)Counter(a).most_common(2)可以打印出数组中出现次数最多的元素。参数2表示的含义是:输出几个出现次数最多的元素。
❺ 如何用python实现英文短文的双词频统计
简单版:
#!/usr/bin/envpython3
importre
importjieba
fromcollectionsimportCounter
fname='counttest.txt'
withopen(fname)asf:
s=f.read()
pattern=re.compile(r'[a-zA-Z]+-?[a-zA-Z]*')
english_words=Counter(pattern.findall(s))
other_words=Counter(jieba.cut(pattern.sub('',s)))
print(' 英乱樱胡文单词统计结果: '+'-'*17)
print(' '.join(['{}:{}'.format(i,j)fori,jinenglish_words.most_common()]))
print(' 中文及符号统计结果: '+'-'*19)
print(' '.join(['{}:{}'.format(i,j)fori,jinother_words.most_common()]))
复杂版:
#!/usr/bin/envpython
#-*-coding:utf-8-*-
from__future__importprint_function,division,unicode_literals
importsys,re,time,os,jieba
fromcollectionsimportCounter
fromdatetimeimportdatetime
classWordCounter(object):
def__init__(self,from_file,to_file=None,coding=None,jieba_cut=None):
'''根据设定的进程数,把文件from_file分割成大小基本相同,数量等同与进程数的文件段,
来读取并统计词频,然后把结果写入to_file中,当其为None时直接打印在终端或命令行上。
Args:
@from_file要读取的文件
@to_file结果要写入的文件
@coding文件的编码方式,默认为采用chardet模块读取前1万个字符来自动判断
@jieba_cut是否启用结巴分词,默认为None
Howtouse:
w=WordCounter('a.txt','b.txt')
w.run()
'''
ifnotos.path.isfile(from_file):
raiseException('Nosuchfile:文件不存在')
self.f1=from_file
self.filesize=os.path.getsize(from_file)
self.f2=to_file
ifcodingisNone:
try:
importchardet
exceptImportError:
os.system('pipinstallchardet')
print('-'*70)
importchardet
withopen(from_file,'rb')asf:
coding=chardet.detect(f.read(10000))['encoding']
self.coding=coding
self._c=[Counter(),Counter()]
self.jieba=False
ifjieba_cutisnotNone:
颂乱self.jieba=True
defrun(self):
start=time.time()
if1:
self.count_direct(self.f1)
ifself.f2notin['None','Null','none','null',None]:
withopen(self.f2,'wb')asf:
f.write(self.result.encode(self.coding))
else:
print(' Englishwords: '+'-'*15)
print(self.result)
cost='哗拦{:.1f}'.format(time.time()-start)
size=humansize(self.filesize)
tip=' Filesize:{}.Costtime:{}seconds'
#print(tip.format(size,cost))
self.cost=cost+'s'
defcount_direct(self,from_file):
'''直接把文件内容全部读进内存并统计词频'''
start=time.time()
withopen(from_file,'rb')asf:
line=f.read()
foriinrange(len(self._c)):
self._c[i].update(self.parse(line)[i])
defparse(self,line):#解析读取的文件流
text=line.decode(self.coding)
text=re.sub(r'- ','',text)#考虑同一个单词被分割成两段的情况,删除行末的-号
pattern=re.compile(r'[a-zA-Z]+-?[a-zA-Z]*')#判断是否为英文单词
english_words=pattern.findall(text)
rest=pattern.sub('',text)
ex=Counter(jieba.cut(rest))ifself.jiebaelseCounter(text)
returnCounter(english_words),ex
defflush(self):#清空统计结果
self._c=[Counter(),Counter()]
@property
defcounter(self):#返回统计结果的Counter类
returnself._c
@property
defresult(self):#返回统计结果的字符串型式,等同于要写入结果文件的内容
ss=[]
forcinself._c:
ss.append(['{}:{}'.format(i,j)fori,jinc.most_common()])
tip=' 中文及符号统计结果: '+'-'*15+' '
returntip.join([' '.join(s)forsinss])
defhumansize(size):
"""将文件的大小转成带单位的形式
>>>humansize(1024)=='1KB'
True
>>>humansize(1000)=='1000B'
True
>>>humansize(1024*1024)=='1M'
True
>>>humansize(1024*1024*1024*2)=='2G'
True
"""
units=['B','KB','M','G','T']
forunitinunits:
ifsize<1024:
break
size=size//1024
return'{}{}'.format(size,unit)
defmain():
iflen(sys.argv)<2:
print('Usage:pythonwordcounter.pyfrom_fileto_file')
exit(1)
from_file,to_file=sys.argv[1:3]
args={'coding':None,'jieba_cut':1}
foriinsys.argv:
forkinargs:
ifre.search(r'{}=(.+)'.format(k),i):
args[k]=re.findall(r'{}=(.+)'.format(k),i)[0]
w=WordCounter(from_file,to_file,**args)
w.run()
if__name__=='__main__':
importdoctest
doctest.testmod()
main()
更复杂的:如果是比较大的文件,建议采用多进程,详情网络:多进程读取大文件并统计词频 jaket5219999