From 5354d6387b357f559ebb324bfa8fe9c12283a767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=96=87=E7=90=B3?= <2509165042@student.example.com> Date: Tue, 21 Apr 2026 11:27:37 +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 --- XWL.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 XWL.py diff --git a/XWL.py b/XWL.py new file mode 100644 index 0000000..c907fc8 --- /dev/null +++ b/XWL.py @@ -0,0 +1,34 @@ +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") \ No newline at end of file