• Cy23
    2023-01-20 来自辽宁
    看了下同学的留言学习了下,并改写正则,使其捕获单词排除换行和空格等 改写如下: from re import findall from collections import Counter with open('./demo1.txt', 'r') as f: data = f.read().lower() words = findall(r'\b\w*?\S\b', data) print(Counter(words)) print(Counter(words).most_common(5))

    作者回复: 赞,不断迭代代码是正确提高编程能力的有效方式

    
    1
  • 谢韬 Fragos
    2023-03-01 来自北京
    如老师提到如果需要累加多个点,比如p1+p2+p3+p4有下面三种方法供大家参考: 第一种方法:在__add__方法中返回Point_2D的类型,而不是tuple。 from collections import namedtuple class Point_2D (namedtuple('Point',['x','y'])): def __add__(self,other): self.other_x, self.other_y= other return self.__class__(self.x+self.other_x,self.y+self.other_y) p1= Point_2D(x=11,y=22) p2 = Point_2D(x=33,y=44) p3 = Point_2D(x=55,y=66) p4 = Point_2D(x=77,y=88) p1+p2+p3+p4 #第二种方法:重载Point_2D的__radd__方法,否则它会以元组的默认加法方式进行。 from collections import namedtuple class Point_2D(namedtuple('Point',['x','y'])): def __add__(self,other): self.other_x , self.other_y = other return self.x + self.other_x, self.y+ self.other_y def __radd__(self,other): self.other_x , self.other_y = other return self.x + self.other_x, self.y+ self.other_y #第三种方法:这段代码还是不够pythonic,__add__和__radd__代码重复了,如果连个方法的表现出一样的行为直接用=就可以了。 from collections import namedtuple class Point_2D(namedtuple('Point',['x','y'])): def __add__(self,other): self.other_x,self.other_y =other return self.x + self.other_x,self.y + self.other_y __radd__=__add__
    展开

    作者回复: 送你上去~

    
    
  • PatrickL
    2023-01-16 来自上海
    import re from collections import Counter with open('hamlet.txt','r') as f: data = f.read().lower() words = re.findall(r'\w+', data) print(Counter(words).most_common(5))
    
    1
  • Geek_Mike
    2023-08-14 来自云南
    # 自定义命名元组的加法和减法功能 from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) def add(self, other): return self.x + other.x, self.y + other.y Point.__add__ = add def subtract(self, other): return self.x - other.x, self.y - other.y Point.__sub__ = subtract p1 = Point(2, 3) p2 = Point(1, 4) print(p1 + p2) print(p1 - p2)
    
    
  • Geek_Mike
    2023-08-14 来自云南
    # 统计一篇文章中出现频率在前五的单词,并将单词和出现次数一起输出到终端 from collections import Counter import re words = re.findall(r'\w+', open('./demo.txt').read()) print(Counter(words).most_common(5)) >>>[('Python', 12), ('3', 12), ('10', 12), ('11', 6), ('is', 6)]
    
    
  • Greenery
    2023-07-24 来自中国香港
    from re import findall with open('demo.txt',encoding='UTF-8') as f: cnt=f.read() c=Counter(findall(r'\w+',cnt)) c.most_common(5)
    
    
  • Mr.Cui
    2023-05-23 来自浙江
    # 请你编写程序,统计一篇文章中出现频率在前五的单词,并将单词和出现次数一起输出到终端。 # 使用 collections容器数据类型中的 Counter 对象 from collections import Counter with open('./case.log',mode='r') as f: content = f.read().lower() for word, count in Counter(content.split()).most_common(5): print(f"{word}: {count}")
    
    
  • 江江儿嘛哩哄
    2023-03-08 来自浙江
    from collections import Counter import re words = re.findall(r'\w+', open('a.txt').read().lower()) print(Counter(words).most_common(5))
    
    
  • yanyu-xin
    2023-02-27 来自广东
    from collections import Counter import re words = re.findall(r'\w+', open('E4.txt',encoding='UTF-8').read().lower()) print(Counter(words).most_common(5))
    
    
  • 朱雯
    2023-02-04 来自北京
    from re import findall from collections import Counter with open('./demo1.txt', 'r') as f: data = f.read().lower() words = findall(r'\b\w*?\S\b', data) print(Counter(words)) print(Counter(words).most_common(5))
    
    