Python 知識回顧,提升技能

Python 知識回顧,提升技能

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @virtualenv:study
# @contact: [email protected]
# @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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章