3-2-1 文本数据处理导论
This commit is contained in:
6
0421+2509165015/1.py
Normal file
6
0421+2509165015/1.py
Normal file
@@ -0,0 +1,6 @@
|
||||
print("=== 1. 打印Hello每个字符的ASCII码 ===")
|
||||
for char in "Hello":
|
||||
print(f"字符 '{char}' 的ASCII码: {ord(char)}")
|
||||
print("\n=== 2. 验证 chr(65) ===")
|
||||
result = chr(65)
|
||||
print(f"chr(65) 的结果是: {result}")
|
||||
28
0421+2509165015/2.py
Normal file
28
0421+2509165015/2.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import math
|
||||
print("===== 题目3 向量运算 =====")
|
||||
A = [3, 4]
|
||||
B = [1, 2]
|
||||
A_plus_B = [a + b for a, b in zip(A, B)]
|
||||
print(f"A + B = {A_plus_B}")
|
||||
two_A = [2 * a for a in A]
|
||||
print(f"2 * A = {two_A}")
|
||||
norm_A = math.sqrt(sum(x**2 for x in A))
|
||||
print(f"A的模长 = {norm_A}")
|
||||
print("\n===== 题目4 点积 & 余弦相似度 =====")
|
||||
A1 = [1, 2, 3]
|
||||
B1 = [4, 5, 6]
|
||||
dot1 = sum(a*b for a, b in zip(A1, B1))
|
||||
norm_A1 = math.sqrt(sum(x**2 for x in A1))
|
||||
norm_B1 = math.sqrt(sum(x**2 for x in B1))
|
||||
cos1 = dot1 / (norm_A1 * norm_B1)
|
||||
print(f"A1·B1 点积 = {dot1}")
|
||||
print(f"A1、B1 余弦相似度 = {cos1:.4f}")
|
||||
A2 = [1, 0]
|
||||
B2 = [0, 1]
|
||||
dot2 = sum(a*b for a, b in zip(A2, B2))
|
||||
norm_A2 = math.sqrt(sum(x**2 for x in A2))
|
||||
norm_B2 = math.sqrt(sum(x**2 for x in B2))
|
||||
cos2 = dot2 / (norm_A2 * norm_B2)
|
||||
print(f"\nA2·B2 点积 = {dot2}")
|
||||
print(f"A2、B2 余弦相似度 = {cos2}")
|
||||
print("原因:两向量正交(夹角90°),余弦值为0")
|
||||
Reference in New Issue
Block a user