3月 132021
 
"""
凯撒密码
"""
# 导入模块
import pyperclip

# 待加密信息字符串赋值变量
message = 'This is my secret message.'
# message = 'guv6Jv6Jz!J6rp5r7Jzr66ntrM'
# 指定密钥赋值变量
key = 13
# 指定模式为加密或解密并赋值变量
mode = 'encrypt'
# mode = 'decrypt'
# 定义字符并赋值变量
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'
# 定义空字符串赋值变量以存储加解密后的信息
translated = ''

# 定义for循环,遍历待加密消息字符串的每一个字符并赋值变量
for symbol in message:
    # 判断,当待加密消息字符串中字符存在于定义字符集(True)时则执行
    if symbol in SYMBOLS:
        # 使用字符串find()方法在字符串中查找子字符串所在索引值并赋值变量
        # https://docs.python.org/3/library/stdtypes.html#str.find
        symbolIndex = SYMBOLS.find(symbol)

        # 判断当前操作模式为加密或者是解密
        if mode == 'encrypt':
            # 加密模式下,索引值加上密钥值赋值给变量(向后移动)
            translatedIndex = symbolIndex + key
        elif mode == 'decrypt':
            # 解密模式下,索引值减去密钥值赋值给变量(向前移动)
            translatedIndex = symbolIndex - key

        # 判断加解密后字符索引值值大于等于自定义字符集长度值
        if translatedIndex >= len(SYMBOLS):
            # 加解密后字符索引值减去自定义字符集长度并重新赋值给加解密后字符索引值
            translatedIndex = translatedIndex - len(SYMBOLS)
        # 判断加解密后字符索引值小于0时
        elif translatedIndex < 0:
            # 加解密后字符索引值加上自定义字符集长度并重新赋值给加解密后字符索引值
            translatedIndex = translatedIndex + len(SYMBOLS)

        # 使用空字符串值依次拼接使用加解密后字符索引值在自定义字符集中的字符并重新赋值给变量
        translated = translated + SYMBOLS[translatedIndex]

    # 当遍历的待加解密字符不存在于字符集时
    else:
        # 使用空字符串值依次拼接使用遍历的原始字符并重新赋值给变量(即不存在的字符不做加解密)
        translated = translated + symbol

# 打印加解密后的拼接字符串
print(translated)
# 调用模块,将结果复制到剪贴板
3月 132021
 
"""
反向密码
"""

# 待加密消息字符串赋值变量
message = 'Three can keep a secret, if two of them are dead.'
# 空字符串赋值变量用以存储反转后的加密信息
translated = ''

# 计算待加密消息字符串长度减1并赋值变量i作为字符串最后一个字符的索引值
i = len(message) - 1
# 定义while循环,直到i小于0(False)时终止
while i >= 0:
    # 当前变量值拼接待加密消息字符串索引字符并赋值给变量
    translated = translated + message[i]
    # 打印每一次循环
    print('i is ', i, ', message i is ', message[i], ', translated is ', translated)
    # 执行i减1并将值重新赋值给变量i
    i = i - 1

# 结束循环后打印反转后的加密信息
print(translated)
3月 022021
 
"""
使用scatter()绘制一系列点
"""
import matplotlib.pyplot as plt

# 向scatter()传递两个分别包含x值和y值的列表
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]

# 列表传递给scatter()时,依次从每个列表中读取一个值来绘制一个点
plt.scatter(x_values, y_values, s=100)

# 设置图表标题并给坐标轴加上标签
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)

plt.tick_params(axis='both', which='major', labelsize=14)

plt.show()
3月 022021
 
"""
使用scatter()绘制散点图
"""
import matplotlib.pyplot as plt

# 绘制单个点,使用scatter()函数并传递一对x和y坐标
# plt.scatter(2, 4)
# 设置坐标并指定点的尺寸
plt.scatter(2, 4, s=200)

# 设置图表标题并给坐标轴加上标签
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)

# 设置刻度标记的大小
plt.tick_params(axis='both', which='major', labelsize=14)

plt.show()
3月 012021
 
"""
https://matplotlib.org/stable/index.html
Matplotlib: Visualization with Python
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
"""
# 导入模块并指定别名
import matplotlib.pyplot as plt

# 提供输入值,解决x坐标从0开始的问题
input_values = [1, 2, 3, 4, 5]
# 定义平方数列表
square = [1, 4, 9, 16, 25]


# 将列表传递给函数
# plt.plot(square)
# 改善图形可读性,线条加粗
# plt.plot(square, linewidth=5)
# 同时提供输入值列表和输出值列表
plt.plot(input_values, square, linewidth=5)


# 函数title()指定图表标题,参数fontsize指定文字大小
plt.title("Square Numbers", fontsize=24)
# 函数xlable()和ylable()为每条轴设置标题并指定文字大小
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)

# 函数tick_parms()设置刻度样式,并将设置刻度文字大小
# axis n. 轴(旋转物体假想的中心线); (尤指图表中的)固定参考轴线,坐标轴; 对称中心线(将物体平分为二);
plt.tick_params(axis='both', labelsize=14)

# 调用matplotlib查看器,显示绘制的图形
plt.show()
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