Files
task-3-2-1-Text-Processing-…/XWL.py
2026-04-21 11:27:37 +08:00

34 lines
1.0 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.

print(" 题目1")
s = "Hello"
for c in s:
print(f"'{c}' 的ASCII码: {ord(c)}")
print(f"ASCII码65对应的字符: {chr(65)}")
import math
print("题目3 ")
A = [3, 4]
B = [1, 2]
A_plus_B = [A[0] + B[0], A[1] + B[1]]
print("A + B =", A_plus_B)
two_times_A = [2 * A[0], 2 * A[1]]
print("2 × A =", two_times_A)
A_norm = math.sqrt(A[0] ** 2 + A[1] ** 2)
print("A 的模 =", A_norm)
print(" 题目4 ")
A = [1, 2, 3]
B = [4, 5, 6]
dot_product = sum(a * b for a, b in zip(A, B))
print("A·B =", dot_product)
def vector_norm(v):
return math.sqrt(sum(x ** 2 for x in v))
norm_A = vector_norm(A)
norm_B = vector_norm(B)
cos_sim = dot_product / (norm_A * norm_B)
print("余弦相似度 =", cos_sim)
A_new = [1, 0]
B_new = [0, 1]
dot_product_new = sum(a * b for a, b in zip(A_new, B_new))
norm_A_new = vector_norm(A_new)
norm_B_new = vector_norm(B_new)
cos_sim_new = dot_product_new / (norm_A_new * norm_B_new)
print("A=[1,0], B=[0,1] 的余弦相似度 =", cos_sim_new)
print("原因:两个向量正交,点积为0,因此余弦相似度为0")