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

29 lines
916 B
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)}")
A=[3,4]
B=[1,2]
add_result=[A[i]+B[i]for i in range(len(A))]
print("题目3-1 A+B结果:",add_result)
mult_result=[2* x for x in A]
print("题目3-2 2*A结果",mult_result)
import math
a_length=math.sqrt(sum(x**2 for x in A))
print("题目3-3 A的长度",a_length)
A1=[1,2,3]
B1=[4,5,6]
dot_product=sum(A1[i]*B1[i]for i in range(len(A1)))
print("\n题目4-1 点积结果:",dot_product)
a_len=math.sqrt(sum(x**2 for x in A1))
b_len=math.sqrt(sum(x**2 for x in B1))
cos_sim=dot_product/(a_len*b_len)
print("题目4-2 余弦相似度:",cos_sim)
A2=[1,0]
B2=[0,1]
dot2=sum(A2[i]*B2[i]for i in range(len(A2)))
a2_len=math.sqrt(sum(x**2 for x in A2))
b2_len=math.sqrt(sum(x**2 for x in B2))
cos_sim2=dot2/(a2_len*b2_len)
print("题目4-3 余弦相似度:",cos_sim2,",原因两个向量正交夹角90度)")