11月 062020
 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time       : 2020/11/3 14:59
# @Author     : Harvey Mei <harvey.mei@msn.com>
# @FileName   : base36.py
# @IDE        : PyCharm
# @GitHub     :https://github.com/harveymei/

import string


# base10 to base36
def encode(number):
    alphabet = string.digits + string.ascii_lowercase
    value = ''

    while number != 0:
        number, index = divmod(number, len(alphabet))
        value = alphabet[index] + value

    return value or '0'


# base36 to base10
def decode(value):
    return int(value, 36)


# 循环,当number > 0时求商取余,将余数作为字符串切片取值
print(divmod(1024, 36))
print((string.digits + string.ascii_lowercase)[16])
# 'g' + ''

print(divmod(28, 36))
print((string.digits + string.ascii_lowercase)[28])
# 's' + 'g'

# 当number == 0时退出循环
print(divmod(0, 36))
print((string.digits + string.ascii_lowercase)[0])
10月 302020
 
# -*- coding:utf-8 -*-
# 类是一系列具有共同特征和行为的事物的抽象概念的总和
# 在类里面赋值的变量就是类的变量,专有术语称之为类的属性(Class Attribute)

class CocaCola:  # Class names should use CamelCase convention
    formula = ['caffeine', 'sugar', 'water', 'soda']


# 类的实例化
# 将类赋值给变量的过程称之为类的实例化,被实例化后的对象称之为实例
coke_for_me = CocaCola()  # PEP 8: E305 expected 2 blank lines after class or function definition, found 1
coke_for_you = CocaCola()

# 类属性引用
# 在类的名字后输入.后IDE自动联想类中的属性
# 类的属性会被所有类的实例共享
print(CocaCola.formula)
print(coke_for_me.formula)
print(coke_for_you.formula)

for element in coke_for_me.formula:
    print(element)

# 实例属性
coke_for_china = CocaCola()
coke_for_china.local_logo = '可口可乐'

print(coke_for_china.local_logo)
C:\Users\harveymei\PycharmProjects\mod3736\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/mod3736/howto.py
['caffeine', 'sugar', 'water', 'soda']
['caffeine', 'sugar', 'water', 'soda']
['caffeine', 'sugar', 'water', 'soda']
caffeine
sugar
water
soda
可口可乐

Process finished with exit code 0
10月 292020
 
# -*- coding:utf-8 -*-
import string

# 输出所有标点符号!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
print(string.punctuation)
# https://docs.python.org/zh-cn/3/library/stdtypes.html#str.strip
# strip()方法返回原字符串的副本,移除其中的前导和末尾字符。不指定参数则移除空格。
# https://docs.python.org/zh-cn/3/library/string.html#string.punctuation
# 字符串常量,返回被视为标点符号的 ASCII 字符所组成的字符串
# https://docs.python.org/zh-cn/3/library/functions.html#func-set
# set()函数,返回集合对象。
# set()函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。


path = 'C:/Users/harveymei/Desktop/news.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)  # 使用set()函数将列表中的字符去重复后赋值变量
    # 循环获取列表元素作为key,并将words列表中元素计数值作为value,生成字典并赋值变量
    counts_dic = {index: words.count(index) for index in words_index}

# https://docs.python.org/zh-cn/3/library/functions.html#sorted
# 根据 iterable 中的项返回一个新的已排序列表。
# 具有两个可选参数,它们都必须指定为关键字参数。
# key 指定带有单个参数的函数,用于从 iterable 的每个元素中提取用于比较的键 (例如 key=str.lower)。 默认值为 None (直接比较元素)。
# reverse 为一个布尔值。 如果设为 True,则每个列表元素将按反向顺序比较进行排序。
# https://docs.python.org/zh-cn/3/howto/sorting.html#key-functions
# key 形参用来指定在进行比较前要在每个列表元素上调用的函数(或其他可调用对象)。
# https://docs.python.org/zh-cn/3/reference/expressions.html?highlight=lambda#lambda
# lambda 表达式(有时称为 lambda 构型)被用于创建匿名函数。

for word in sorted(counts_dic, key=lambda x: counts_dic[x], reverse=True):  # 循环对字典中元素按照计数值反向排序并赋值变量
    print('{} -- {} times'.format(word, counts_dic[word]))  # 使用格式化字符串打印单词及对应计数值
C:\Users\harveymei\PycharmProjects\mod3736\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/mod3736/static.py
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
the -- 14 times
a -- 4 times
in -- 4 times
and -- 4 times
that -- 4 times
leads -- 2 times
created -- 2 times
practice -- 2 times
such -- 2 times
china -- 2 times
has -- 2 times
said -- 2 times
legislature -- 2 times
party -- 2 times
li -- 2 times
experience -- 2 times
process -- 2 times
is -- 2 times
adding -- 2 times
government -- 2 times
roles -- 2 times
good -- 2 times
governance -- 2 times
cpc -- 2 times
due -- 2 times
work -- 2 times
formulation -- 2 times
their -- 2 times
of -- 2 times
play -- 2 times

Process finished with exit code 0
10月 282020
 
# -*- coding:utf-8 -*-
lyric = 'The night begin to shine, the night begin to shine'
words = lyric.split()
print(words)

# 词频统计
# https://docs.python.org/zh-cn/3/library/stdtypes.html#str.split
# split()方法:返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串
# https://docs.python.org/zh-cn/3/library/stdtypes.html#str.count
# count()方法:返回子字符串 sub 在 [start, end] 范围内非重叠出现的次数。 可选参数 start 与 end 会被解读为切片表示法。
# https://docs.python.org/zh-cn/3/tutorial/inputoutput.html#methods-of-file-objects
# 文件对象的read()方法,读取文件内容
path = 'C:/Users/harveymei/Desktop/news.txt'
with open(path, 'r') as text:  # 只读打开文件并赋值给变量text
    words = text.read().split()  # 读取文件内容拆分单词并赋值变量
    print(words)
    for word in words:  # for循环遍历列表中元素并赋值给变量
        print('{} -{} times'.format(word, words.count(word)))  # 将元素及元素计数插入字符串

# 问题:
# 1,带标点符号的单词被单独统计
# 2,一些单词展示多次统计
# 3,首字母大写单词被单独统计
C:\Users\harveymei\PycharmProjects\mod3736\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/mod3736/static.py
['The', 'night', 'begin', 'to', 'shine,', 'the', 'night', 'begin', 'to', 'shine']
['The', 'Party', 'leads', 'the', 'formulation', 'work,', 'and', 'the', 'government', 'and', 'the', 'legislature', 'play', 'their', 'due', 'roles', 'in', 'the', 'process,', 'Li', 'said,', 'adding', 'that', 'such', 'a', 'practice', 'is', 'a', 'good', 'experience', 'that', 'the', 'CPC', 'has', 'created', 'in', 'the', 'governance', 'of', 'China.', 'The', 'Party', 'leads', 'the', 'formulation', 'work,', 'and', 'the', 'government', 'and', 'the', 'legislature', 'play', 'their', 'due', 'roles', 'in', 'the', 'process,', 'Li', 'said,', 'adding', 'that', 'such', 'a', 'practice', 'is', 'a', 'good', 'experience', 'that', 'the', 'CPC', 'has', 'created', 'in', 'the', 'governance', 'of', 'China.']
The -2 times
Party -2 times
leads -2 times
the -12 times
formulation -2 times
work, -2 times
and -4 times
the -12 times
government -2 times
and -4 times
the -12 times
legislature -2 times
play -2 times
their -2 times
due -2 times
roles -2 times
in -4 times
the -12 times
process, -2 times
Li -2 times
said, -2 times
adding -2 times
that -4 times
such -2 times
a -4 times
practice -2 times
is -2 times
a -4 times
good -2 times
experience -2 times
that -4 times
the -12 times
CPC -2 times
has -2 times
created -2 times
in -4 times
the -12 times
governance -2 times
of -2 times
China. -2 times
The -2 times
Party -2 times
leads -2 times
the -12 times
formulation -2 times
work, -2 times
and -4 times
the -12 times
government -2 times
and -4 times
the -12 times
legislature -2 times
play -2 times
their -2 times
due -2 times
roles -2 times
in -4 times
the -12 times
process, -2 times
Li -2 times
said, -2 times
adding -2 times
that -4 times
such -2 times
a -4 times
practice -2 times
is -2 times
a -4 times
good -2 times
experience -2 times
that -4 times
the -12 times
CPC -2 times
has -2 times
created -2 times
in -4 times
the -12 times
governance -2 times
of -2 times
China. -2 times

Process finished with exit code 0
10月 282020
 
# -*- coding:utf-8 -*-
# 列表推导式 == 列表解析式
# list = [ item for item in iterable ]
# iterate:迭代
# 放在列表中的元素,就是后面循环的元素本身

a = [i ** 2 for i in range(1, 10)]  # 将1-9的平方值依次放入列表
c = [j + 1 for j in range(1, 10)]  # 将1-9分别加1的值放入列表
k = [n for n in range(1, 10) if n % 2 == 0]  # 依次判断1-10中是否为偶数并将偶数值放入列表
z = [letter.lower() for letter in 'ABCEDFGHIJKLMN']  # 将字符串中的大写字母依次转换为小写并放入列表
print(a, c, k, z)

# 字典推导式
d = {i: i+1 for i in range(4)}  # 将0-3依次作为key,0-3分别加1作为value,放入字典
print(d)
g = {i: j for i, j in zip(range(1, 6), 'abcde')}  # 将1-5依次作为key,将字符串中字符依次作为value,放入字典
print(g)
m = {i: j.upper() for i, j in zip(range(1, 6), 'abcde')}  # 将1-5依次作为key,将字符依次作为value并转换大小,放入字典
print(m)

# enumerate:枚举
# 循环列表时获取元素的索引
# 列表是有序的
# enumerate()函数,参数为可迭代对象,返回一个枚举对象。
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
for num, letter in enumerate(letters):  # 返回一个包含计数值(默认从0开始)和通过迭代获得的值
    print(letter, 'is', num + 1)

# 示例
print(list(enumerate(letters)))
print(list(enumerate(letters, start=1)))
C:\Users\harveymei\PycharmProjects\mod3736\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/mod3736/news.py
[1, 4, 9, 16, 25, 36, 49, 64, 81] [2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 4, 6, 8] ['a', 'b', 'c', 'e', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
{0: 1, 1: 2, 2: 3, 3: 4}
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
{1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
a is 1
b is 2
c is 3
d is 4
e is 5
f is 6
g is 7
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g')]
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g')]

Process finished with exit code 0
10月 262020
 
# 空白泛指任何非打印字符,如空格,制表符和换行

# 在字符串中添加制表符\t
print("Python")
print("\tPython")

# 在字符串中添加换行符\n
print("Languages:\nPython\nC\nJavaScript")

# 在同一个字符串中可以同时包含制表符和换行符
print("Languages:\n\tPython\n\tC\n\tJavaScript")
D:\PycharmProjects\pythonProject2\venv\Scripts\python.exe D:/PycharmProjects/pythonProject2/hello2.py
Python
	Python
Languages:
Python
C
JavaScript
Languages:
	Python
	C
	JavaScript

Process finished with exit code 0
10月 262020
 
# 变量名只能包含字母,数字和下划线
# 变量名能以字母和下划线开头,但不能以数字开头
# 变量名不能包含空格

# 使用字符串的.title()方法修改大小写
name = 'harvey mei'
print(name.title())  # 该方法以首字母大写的方式显示每个单词

# 将字符串全部改为大写或小写
name = 'Ada Lovelace'
print(name.upper())  # 大写
print(name.lower())  # 小写

# 在字符串中使用变量
first_name = 'tom'
last_name = 'chen'
full_name = f"{first_name} {last_name}"  # f字符串在python3.6引入
print(full_name)

first_name = 'alice'
last_name = 'wang'
full_name = "{} {}".format(first_name, last_name)  # 使用python3.5或更早版本需要使用format()方法
print(full_name)
D:\PycharmProjects\pythonProject2\venv\Scripts\python.exe D:/PycharmProjects/pythonProject2/hello1.py
Harvey Mei
ADA LOVELACE
ada lovelace
tom chen
alice wang

Process finished with exit code 0
10月 262020
 
False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

https://docs.python.org/zh-cn/3/reference/lexical_analysis.html#keywords
10月 222020
 
# 列表操作方式的效率比较
import time

a = []
# t0 = time.clock()  # 异常:AttributeError: module 'time' has no attribute 'clock'
# https://docs.python.org/3/whatsnew/3.8.html
# https://docs.python.org/3/library/time.html#time.perf_counter
# 该函数在python3.3废弃,在python3.8移除
t0 = time.perf_counter()
for i in range(1, 20000):
    a.append(i)
print(time.perf_counter() - t0, 'seconds process time')

t0 = time.perf_counter()
b = [i for i in range(1, 20000)]
print(time.perf_counter() - t0, 'seconds process time')
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/hello.py
0.0017573999999999992 seconds process time
0.0005343000000000014 seconds process time

Process finished with exit code 0
# 列表操作方式的效率比较
import time

a = []
# t0 = time.clock()  # 异常:AttributeError: module 'time' has no attribute 'clock'
# https://docs.python.org/3/whatsnew/3.8.html
# https://docs.python.org/3/library/time.html#time.perf_counter
# 该函数在python3.3废弃,在python3.8移除
t0 = time.perf_counter()  # 调用函数返回精确浮点性能计数值,两次调用返回值的差计算程序运行时间
for i in range(1, 20000):
    a.append(i)
t1 = time.perf_counter()
print('T0 is', t0)
print('T1 is', t1)
print(t1 - t0, 'seconds process time')

t0 = time.perf_counter()
b = [i for i in range(1, 20000)]
print(time.perf_counter() - t0, 'seconds process time')
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/hello.py
T0 is 0.0509396
T1 is 0.052513
0.0015733999999999956 seconds process time
0.0005889999999999992 seconds process time

Process finished with exit code 0
10月 222020
 
# -*- coding:utf-8 -*-
# 数据结构,包括列表lists,字典dictionaries,元组tuples,集合sets

# 数据结构中的推导式或列表解析式
# 将10个元素装入列表,普通写法
a = []
for i in range(1, 11):
    a.append(i)
print(a)

# 列表解析式写法(方便,高效)
b = [i for i in range(1, 11)]
print(b)
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/datastructure.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Process finished with exit code 0