Python 知识回顾,提升技能

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @virtualenv:study
# @contact: 1040691703@qq.com
# @Desc:大数据比赛-第一周测试
__author__ = '未昔/AngelFate'
__date__ = '2019/11/9 13:52'

class Test(object):
      local = '苏州' 
    def __init__(self):
        self.name = 'wangwei' 
        self._age = 21 
        self.__tel = 852685 

    def A_SanYuan(self):
        """
        三元表达式
        :return:
        """
        a = [1,2]
        b = [3,4]
        c = a if a==b else b  
        print(c)

    def B_ListJiexi(self): 
        a = [i for i in range(1,5)]
        print( a: ', a)
        b = [i for i in range(1, 5) if i%2==0]
        print('b: ', b)
        spider_url = ['http://www.sisoww.cn:5202/blog/detail/{}'.format(str(ii)) for ii in range(3)]
        print('spider_url:', spider_url, type(spider_url))


        test = tuple(i**2 for i in range(1,5))
        print('\n test:',test, type(test))
        test2 = (i ** 2 for i in range(1, 5))
        print('test2: ',test2, type(test2)) 

        test3 = {a:ord(a) for a in 'python'} 
        print('\n test3: ', test3, type(test3))
        test3_1 = {a: ord(a) for a in 'python' if a!='p'}
        print('test3_1: ', test3_1, type(test3_1))

        test4 = {i**2 for i in range(1,10) if i>5}
        print('\n test4: ', test4, type(test4))

        test5 = max([x for x in ('a','B''0','$')])  
        print('\n test5: ', test5, type(test5))
        test5_1 = min({x for x in ('a','B''0','$')})
        print(test5_1, {test5_1:ord(test5_1)})
        test5_2 = sorted((x for x in ('a','B','0','&'))) 
        print(test5_2)


    def C_Iter(self):  
        a = iter({'name':'wangwei','tel':18501490000}.items())  
        # print(a,type(a))
        print(next(a))
        print(next(a))

    def D_MapFilter(self):
        test = map(lambda x:x**2,[1,2,3,4,5])
        print('test: ',test)
        print('next(test): ',next(test))
        print('next(test): ', next(test))
        print('next(test): ', next(test))
        print('list(test): ', list(test))

        test2 = filter(lambda x:x%2==1,range(1,11,1))
        print('\ntest2: ', test2)
        print('list(test2): ', list(test2))

    def E_ZiFu(self):  
        chinese = 0  
        alpha = 0 
        space = 0 
        digit = 0 
        others = 0 
        test = '艺赛旗wangwei,王aa 1210.'
        for i in test:
            if i.isalpha(): 
                if ('\u4e00' <=i <= '\u9fa5'): 
                    chinese +=1
                else:
                    alpha += 1
            elif i.isspace(): 
                space +=1
            elif i.isdigit():  
                digit+=1
            else:
                others +=1
        print(chinese,alpha,space,digit,others)

    def F_Jieba_Test(self): # 简单的jieba测试
        import jieba
        test = '我教你求仙问卜,趋吉避凶之术,好吗师傅,似这般可得长生吗不能,不能求仙问卜,不如自己做主,不学不学那我教你念佛诵经,朝真降圣,可好可得长生吗好似水中捞月师傅说话不爽快,我是个老实人,不会求仙,不会打隐语,什么叫做水中捞月,不学不学,打坐参禅不如弄棒打拳,师傅,再换个别的吧你这个猢狲,这也不学,那也不学,我叫你尝尝戒尺的厉害'
        # print(test.split())
        words = jieba.lcut(test)
        print(jieba.lcut(test))  
        print(jieba.lcut(test,True)) 
        print(jieba.lcut_for_search(test)) 

        counts = dict() 
        for i in words:
            if len(i) ==1:
                continue
            else:
                counts[i] = counts.get(i,0)+1  
        print(counts)

        items = list(counts.items()) 
        items.sort(key=lambda x:x[1],reverse=True) 
        print(items)

        drop = '不会,不如,不能'
        counts2 = dict()
        for i in words:
            if len(i) ==1 or i in drop:
                continue
            elif i =='不学':
                ri = 'ri不学'
            elif i == '师傅':
                ri = 'ri唐玄奘'
            else:
                ri = i
        counts2[ri] = counts2.get(ri,0) +1 
        items2 = list(counts2.items())
        items2.sort(key=lambda x:x[1],reverse=True)
		print(items2)
        for i in range(10):
            word,freq = items2[i]
            print('%s--> %d' %(word,freq))



if __name__ == '__main__':
       test = Test()
    print(test._age,test.name)
    test._age = 20
    try:
        print(test._age)
        print(test._Test__tel) 
        print(test.__tel) 
    except Exception as e:
        print(e)
    test.A_SanYuan()
    test.B_ListJiexi()
    test.C_Iter()
    test.E_ZiFu()
    test.D_MapFilter()
    test.F_Jieba_Test()

结果

D:\import\python3.7\python.exe E:/BigData-大数据比赛/第一周测验python/Test.py
21 wangwei
20
852685
'Test' object has no attribute '__tel'
[3, 4]

 a:  [1, 2, 3, 4]
b:  [2, 4]
spider_url: ['http://www.sisoww.cn:5202/blog/detail/0', 'http://www.sisoww.cn:5202/blog/detail/1', 'http://www.sisoww.cn:5202/blog/detail/2'] <class 'list'>

 test: (1, 4, 9, 16) <class 'tuple'>
test2:  <generator object Test.B_ListJiexi.<locals>.<genexpr> at 0x0000012E19E746D8> <class 'generator'>

 test3:  {'p': 112, 'y': 121, 't': 116, 'h': 104, 'o': 111, 'n': 110} <class 'dict'>
test3_1:  {'y': 121, 't': 116, 'h': 104, 'o': 111, 'n': 110} <class 'dict'>

 test4:  {64, 49, 36, 81} <class 'set'>

 test5:  a <class 'str'>
$ {'$': 36}
['&', '0', 'B', 'a']
('name', 'wangwei')
('tel', 18501490000)
4 9 1 4 2
test:  <map object at 0x0000012E110865C0>
next(test):  1
next(test):  4
next(test):  9
list(test):  [16, 25]

test2:  <filter object at 0x0000012E11086668>
list(test2):  [1, 3, 5, 7, 9]
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\10406\AppData\Local\Temp\jieba.cache
Loading model cost 0.883 seconds.
Prefix dict has been built succesfully.
['我教', '你', '求仙', '问卜', ',', '趋吉避凶', '之术', ',', '好', '吗', '师傅', ',', '似', '这般', '可', '得', '长生', '吗', '不能', ',', '不能', '求仙', '问卜', ',', '不如', '自己', '做主', ',', '不学', '不学', '那', '我', '教', '你', '念佛', '诵经', ',', '朝真降', '圣', ',', '可好', '可', '得', '长生', '吗', '好似', '水中捞月', '师傅', '说话', '不爽快', ',', '我', '是', '个', '老实人', ',', '不会', '求仙', ',', '不会', '打', '隐语', ',', '什么', '叫做', '水中捞月', ',', '不学', '不学', ',', '打坐', '参禅', '不如', '弄', '棒', '打拳', ',', '师傅', ',', '再换', '个别', '的', '吧', '你', '这个', '猢狲', ',', '这', '也', '不学', ',', '那', '也', '不学', ',', '我', '叫', '你', '尝尝', '戒尺', '的', '厉害']
['我', '教', '你', '求仙', '问卜', '', '', '趋吉避凶', '之', '术', '', '', '好', '吗', '师傅', '', '', '似', '这般', '可得', '长生', '吗', '不能', '', '', '不能', '求仙', '问卜', '', '', '不如', '自己', '做主', '', '', '不学', '不学', '那', '我', '教', '你', '念佛', '诵经', '', '', '朝', '真', '降', '圣', '', '', '可好', '可得', '长生', '吗', '好似', '似水', '水中', '水中捞月', '捞月', '师傅', '说话', '不爽', '不爽快', '爽快', '', '', '我', '是', '个', '老实', '老实人', '', '', '不会', '求仙', '', '', '不会', '打', '隐语', '', '', '什么', '叫做', '水中', '水中捞月', '捞月', '', '', '不学', '不学', '', '', '打坐', '参禅', '不如', '弄', '棒打', '打拳', '', '', '师傅', '', '', '再', '换', '个别', '别的', '吧', '你', '这个', '猢狲', '', '', '这', '也', '不学', '', '', '那', '也', '不学', '', '', '我', '叫', '你', '尝尝', '戒尺', '的', '厉害']
['我教', '你', '求仙', '问卜', ',', '趋吉避凶', '之术', ',', '好', '吗', '师傅', ',', '似', '这般', '可', '得', '长生', '吗', '不能', ',', '不能', '求仙', '问卜', ',', '不如', '自己', '做主', ',', '不学', '不学', '那', '我', '教', '你', '念佛', '诵经', ',', '朝真降', '圣', ',', '可好', '可', '得', '长生', '吗', '好似', '水中', '捞月', '水中捞月', '师傅', '说话', '不爽', '爽快', '不爽快', ',', '我', '是', '个', '老实', '老实人', ',', '不会', '求仙', ',', '不会', '打', '隐语', ',', '什么', '叫做', '水中', '捞月', '水中捞月', ',', '不学', '不学', ',', '打坐', '参禅', '不如', '弄', '棒', '打拳', ',', '师傅', ',', '再换', '个别', '的', '吧', '你', '这个', '猢狲', ',', '这', '也', '不学', ',', '那', '也', '不学', ',', '我', '叫', '你', '尝尝', '戒尺', '的', '厉害']
{'我教': 1, '求仙': 3, '问卜': 2, '趋吉避凶': 1, '之术': 1, '师傅': 3, '这般': 1, '长生': 2, '不能': 2, '不如': 2, '自己': 1, '做主': 1, '不学': 6, '念佛': 1, '诵经': 1, '朝真降': 1, '可好': 1, '好似': 1, '水中捞月': 2, '说话': 1, '不爽快': 1, '老实人': 1, '不会': 2, '隐语': 1, '什么': 1, '叫做': 1, '打坐': 1, '参禅': 1, '打拳': 1, '再换': 1, '个别': 1, '这个': 1, '猢狲': 1, '尝尝': 1, '戒尺': 1, '厉害': 1}
[('不学', 6), ('求仙', 3), ('师傅', 3), ('问卜', 2), ('长生', 2), ('不能', 2), ('不如', 2), ('水中捞月', 2), ('不会', 2), ('我教', 1), ('趋吉避凶', 1), ('之术', 1), ('这般', 1), ('自己', 1), ('做主', 1), ('念佛', 1), ('诵经', 1), ('朝真降', 1), ('可好', 1), ('好似', 1), ('说话', 1), ('不爽快', 1), ('老实人', 1), ('隐语', 1), ('什么', 1), ('叫做', 1), ('打坐', 1), ('参禅', 1), ('打拳', 1), ('再换', 1), ('个别', 1), ('这个', 1), ('猢狲', 1), ('尝尝', 1), ('戒尺', 1), ('厉害', 1)]
[('ri不学', 6), ('求仙', 3), ('ri唐玄奘', 3), ('问卜', 2), ('长生', 2), ('水中捞月', 2), ('我教', 1), ('趋吉避凶', 1), ('之术', 1), ('这般', 1), ('自己', 1), ('做主', 1), ('念佛', 1), ('诵经', 1), ('朝真降', 1), ('可好', 1), ('好似', 1), ('说话', 1), ('不爽快', 1), ('老实人', 1), ('隐语', 1), ('什么', 1), ('叫做', 1), ('打坐', 1), ('参禅', 1), ('打拳', 1), ('再换', 1), ('个别', 1), ('这个', 1), ('猢狲', 1), ('尝尝', 1), ('戒尺', 1), ('厉害', 1)]
ri不学--> 6
求仙--> 3
ri唐玄奘--> 3
问卜--> 2
长生--> 2
水中捞月--> 2
我教--> 1
趋吉避凶--> 1
之术--> 1
这般--> 1

Process finished with exit code 0