Files
task-3-2-1-Text-Processing-…/hh.py
2026-04-23 16:02:54 +08:00

128 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 实际演示:查看字符的编码值
# 英文例子
text_en = "Hello"
print("=" * 50)
print("英文文本的字符编码")
print("=" * 50)
print(f"文本: {text_en}")
print(f"每个字符的ASCII码: {[ord(c) for c in text_en]}")
print()
# 逐个显示
for c in text_en:
print(f" '{c}' -> {ord(c)}")
print()
print("=" * 50)
print("中文文本的字符编码")
print("=" * 50)
# 中文例子
text_cn = "你好"
print(f"文本: {text_cn}")
print(f"每个字符的UTF-8编码值: {[ord(c) for c in text_cn]}")
print()
# 逐个显示
for c in text_cn:
print(f" '{c}' -> {ord(c)}")
# 用chr()函数反向验证:数字编码转字符
print("验证:数字编码转字符")
print()
# 65是大写字母A
print(f"chr(65) = '{chr(65)}' # 应该是大写字母 A")
# 97是小写字母a
print(f"chr(97) = '{chr(97)}' # 应该是小写字母 a")
# 20013是中文"中"
print(f"chr(20013) = '{chr(20013)}' # 应该是中文''")
# 25991是中文"文"
print(f"chr(25991) = '{chr(25991)}' # 应该是中文''")
print("=" * 50)
print("练习题1答案")
print("=" * 50)
# 1. 用 ord() 函数打印 "Hello" 每个字符的ASCII码
print("1. 'Hello' 的ASCII码:")
print([ord(c) for c in "Hello"])
# 2. 验证字符65对应大写字母A
print()
print("2. 验证 chr(65):")
print(f"chr(65) = '{chr(65)}'")
# 验证范围
print()
print("验证 A-Z 的ASCII码范围 (65-90):")
print([chr(i) for i in range(65, 91)])
import numpy as np
print("=" * 50)
print("NumPy向量创建演示")
print("=" * 50)
# 一维向量只有1个数字
v1 = np.array([3])
print(f"一维向量 v1 = {v1}")
print(f"v1 有 {len(v1)} 个元素")
# 二维向量2个数字表示平面上的一个点
v2 = np.array([2, 3])
print(f"\n二维向量 v2 = {v2}")
print(f"v2 有 {len(v2)} 个元素")
# 三维向量3个数字表示立体空间的一个点
v3 = np.array([1, 2, 3])
print(f"\n三维向量 v3 = {v3}")
print(f"v3 有 {len(v3)} 个元素")
# 高维向量(机器学习中常用,几十维到几千维)
v10 = np.array([0.1, 0.5, -0.3, 0.8, 0.2, -0.1, 0.7, 0.3, -0.2, 0.6])
print(f"\n10维向量 v10 = {v10}")
print(f"v10 有 {len(v10)} 个元素")
import numpy as np
import matplotlib.pyplot as plt
# 设置中文字体(如果系统有的话)
try:
plt.rcParams['font.sans-serif'] = ['SimHei', 'Noto Sans CJK SC', 'WenQuanYi Micro Hei']
plt.rcParams['axes.unicode_minus'] = False
except:
pass # 如果没有中文字体就用默认
# 创建画布
fig, ax = plt.subplots(figsize=(8, 8))
# 定义向量
vectors = {
'A = [2, 3]': np.array([2, 3]),
'B = [4, 1]': np.array([4, 1]),
'C = [1, 1]': np.array([1, 1]),
}
# 画每个向量
colors = ['red', 'blue', 'green']
for (name, vec), color in zip(vectors.items(), colors):
ax.annotate('', xy=vec, xytext=(0, 0),
arrowprops=dict(arrowstyle='->', color=color, lw=2))
ax.text(vec[0]+0.1, vec[1]+0.1, name, fontsize=12, color=color)
# 画坐标系
ax.axhline(y=0, color='black', linewidth=0.5)
ax.axvline(x=0, color='black', linewidth=0.5)
# 设置范围
ax.set_xlim(-0.5, 5.5)
ax.set_ylim(-0.5, 4)
ax.set_xlabel('x (abscissa)', fontsize=12)
ax.set_ylabel('y (ordinate)', fontsize=12)
ax.set_title('2D Vector Visualization', fontsize=14)
ax.grid(True, alpha=0.3)
ax.set_aspect('equal')
plt.show()
print("Note: Arrows represent vectors. Endpoint of arrow = vector endpoint")