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

41 lines
1.1 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.

s = "Hello"
for char in s:
print(f"字符'{char}'的ASCII码为: {ord(char)}")
print(f"ASCII码65对应的字符为: {chr(65)}")
##图像是规则数值矩阵易处理,文本是离散符号序列,需理解语义更难
# 题目3
A = [3, 4]
B = [1, 2]
def vector_add(a, b):
return [x + y for x, y in zip(a, b)]
def scalar_multiply(scalar, vector):
return [scalar * x for x in vector]
def vector_norm(vector):
return sum(x**2 for x in vector) ** 0.5
print("题目3:")
print("A + B =", vector_add(A, B))
print("2 × A =", scalar_multiply(2, A))
print("|A| =", vector_norm(A))
A4 = [1, 2, 3]
B4 = [4, 5, 6]
def dot_product(a, b):
return sum(x * y for x, y in zip(a, b))
def cosine_similarity(a, b):
norm_a = vector_norm(a)
norm_b = vector_norm(b)
if norm_a == 0 or norm_b == 0:
return 0.0
return dot_product(a, b) / (norm_a * norm_b)
print("\n题目4:")
print("A · B =", dot_product(A4, B4))
print("余弦相似度 =", cosine_similarity(A4, B4))
A5 = [1, 0]
B5 = [0, 1]
print("\n题目4 第3问:")
print("A = [1, 0], B = [0, 1] 的余弦相似度 =", cosine_similarity(A5, B5))