From 1156d84028df9b638bfe4a234e22d80cb8db1cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BF=9B=E9=B9=8F?= <2509165001@student.example.com> Date: Tue, 21 Apr 2026 11:27:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ljp.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ljp.py diff --git a/ljp.py b/ljp.py new file mode 100644 index 0000000..db5c7f0 --- /dev/null +++ b/ljp.py @@ -0,0 +1,43 @@ +# 1. 输出 "Hello" 每个字符的 ASCII 码 +text = "Hello" +print([ord(c) for c in text]) + +# 2. chr() 验证 65 = A +print(chr(65)) + +# 3. 向量运算 +A = (3, 4) +B = (1, 2) +print("A+B =", (A[0]+B[0], A[1]+B[1])) +print("2*A =", (2*A[0], 2*A[1])) +print("A的模长 =", (A[0]**2 + A[1]**2)**0.5) + +# 4. 点积与余弦相似度 +import math +A = [1,2,3] +B = [4,5,6] +dot = sum(a*b for a,b in zip(A,B)) +print("点积 =", dot) + +normA = math.sqrt(sum(x**2 for x in A)) +normB = math.sqrt(sum(x**2 for x in B)) +cos_sim = dot / (normA * normB) +print("余弦相似度 =", round(cos_sim, 4)) + +# 垂直向量余弦相似度 +A2, B2 = [1,0], [0,1] +dot2 = sum(a*b for a,b in zip(A2,B2)) +normA2 = math.sqrt(sum(x**2 for x in A2)) +normB2 = math.sqrt(sum(x**2 for x in B2)) +cos_sim2 = dot2 / (normA2 * normB2) +print("垂直向量余弦相似度 =", cos_sim2) + +# 5. 词袋模型向量 +vocab = ['java','python','编程','语言'] +doc1 = [0,1,1,1] +doc2 = [1,0,1,1] +doc3 = [0,3,0,0] +print("词表:", vocab) +print("Doc1:", doc1) +print("Doc2:", doc2) +print("Doc3:", doc3) \ No newline at end of file