# 03-CITS1401 Computational Thinking with Pytho

AI悦创原创
• Python 1v1
• Python 1v1

## #CITS1401-Project-2-S2-2022.pdf

Project 2:

Submission deadlines: 5:00 pm, Friday 21****st October 2022

Value: 20% of CITS1401.

To be completed individually.

You are expected to have read and understood the University's guidelines on academic conduct. Following this policy, you may discuss with other students the general principles required to understand this project, but the work you submit must be the result of your own effort. Plagiarism detection, and other systems for detecting potential malpractice, will therefore be used. Besides, if what you submit is not your own work then you will have learned little and will, therefore, likely, fail the final exam.

You must submit your project before the submission deadline listed above. Following UWA policy, a late penalty of 5% will be deducted for each day (24 hours), after the deadline, that the assignment is submitted. No submissions will be allowed after 7 days following the deadline except approved special consideration cases.

Overview

Impressed by your performance in Project-1, the scientists at UWA seek your help in another exciting project. Historically, facial asymmetry and distances between certain facial points (also called) landmarks have been linked to facial beauty and attractiveness (you can read this paper to find out more). This project will help the scientists in measuring facial asymmetry and distances between certain landmarks on the face.

Your task is to find out facial asymmetry between 6 significant facial landmarks and also calculate 6 linear distances between certain landmarks on the face. Facial asymmetry is calculated by flipping the face on its longer axis and subtracting the mirrored landmark values from the original. Table 1 provides the details of each landmark, while Figures 1 and 2 show their location on the original and mirrored face. Table-2 gives you the details of the distances to be calculated. Remember these distances are between the landmarks mentioned in Table-1. Since the face is flipped on it axis, the asymmetry at the nose tip must always be zero i.e. 3D linear distance between “Prn” on original and mirrored faces is zero. We will use the asymmetry of this landmark as a control point.

In this project, you are required to write a computer program that can read the data from a CSV (comma separated values) file provided to you. The file contains the 3D coordinates in X, Y and Z axes for the 7 facial landmarks mentioned in Table 1 for each subject. Yourtask is to write a program which fulfills the following requirements.

Specification: What your program is required to do

Input:

Your program must define the function main with the following syntax:

`def main(csvfile, SubjIDs): `

The input arguments to this function are:

• csvfile: The name oftheCSVfile containing the facial data recordwhich needs to be analysed. Below are the first two rows of the sample file.

csvfile:包含需要分析的面部数据记录的 csv 文件的名称。

The first row of the CSV file contains the following headers:

CSV 文件的第一行包含以下标题:

• SubjID: The identity of a human subject.

SubjID:人类主体的身份。

• Landmark: The facial landmark as mentioned in Table 1.

• “OX”, “OY” and “OZ”: The 3D location of the landmark in X, Y and Z axes respectively on the original face (See Figure-1 (left)).

“OX”、“OY”、“OZ”:分别是该标志在原面上X、Y、Z轴的三维位置(见图1(左))。

• “MX”, “MY” and “MZ”: The 3D location of the landmark in X, Y and Z axes respectively on the mirrored face (See Figure-1 (right)).

“MX”、“MY”和“MZ”:分别是标志点在镜像面上X、Y、Z轴的三维位置(见图1(右))。

• We do not have prior knowledge about the number of subjects we have to analyse (i.e. the number of rows) that the CSV file contains. Also, we are not aware of the order of the columns, so your program needs to check for the column heading to retrieve respective information. The columns ‘SubjID’ and ‘Landmark’ are strings while the remaining data is numeric.

• Note: The X, Y and Z coordinates are in millimetres and need to be within the bounds [-200,200].

• SubjIDs: A list of two IDs of the subjects which need to be analysed. Remember that the ID is a string and is case insensitive.

SubjIDs:需要分析的主题的两个id的列表。请记住，ID是字符串，不区分大小写。

Output:

The function is required to return the following outputs in the order provided below. For ease of

description, we will refer to the input SubjID containing two IDs: “F1” and “F2” as [“F1”,”F2”].

• OP1: A list of two dictionaries containing the facial asymmetry values between the original and mirrored face for the landmarks mentioned in Table-1 for each face F1 and F2 respectively. The keys in the dictionaries are the abbreviations (upper case) of the landmarks (e.g. EX, FT etc.) and their values contain the 3D asymmetry between the original and mirrored landmarks. The formula to calculate the 3D asymmetry is given at the end of this project sheet.

**OP1:**两个字典的列表，包含表1中提到的地标的原始脸和镜像脸之间的面部不对称值，分别针对每个脸F1和F2。字典中的键是地标的缩写(大写)(例如EX, FT等)，它们的值包含原始地标和镜像地标之间的3D不对称。本文在项目表的最后给出了三维非对称性的计算公式。

• OP2: A list of two dictionaries containing the facial distances (as mentioned in Table-2) for each face F1 and F2 respectively. The keys in the dictionaries are the abbreviations (upper case) of the distances (e.g. EXEN, ENAL etc.) and their values contain the 3D Euclidean distance between the corresponding landmarks (see last two columns of Table-2) on the original face. The formula to calculate the Euclidean distance between two 3D landmarks is given at the end of this project sheet.

**OP2:**两个字典的列表，分别包含每个面部F1和F2的面部距离(如表2所示)。字典中的键是距离的缩写(大写)(例如EXEN, ENAL等)，它们的值包含原始表面上相应地标(参见表2的最后两列)之间的3D欧氏距离。计算两个三维地标之间的欧氏距离的公式在项目表的最后给出。

• OP3: First calculate the total facial asymmetries of each subject in the CSV file. Your task is to return a list of Tuple sequences of the 5 faces having the lowest total face asymmetry. The first member of each tuple is the “SubjID” of the face while the second member is the total asymmetry of this face. The list must be in increasing order of total facial asymmetry such that the first tuple would indicate the face that has the lowest total facial asymmetry. Therefore, the 5 tuple sequences will represent 5 lowest total facial asymmetries.

**OP3:**首先计算CSV文件中每个受试者的面部不对称总数。您的任务是返回一个Tuple序列的列表，其中包含5个总面孔不对称程度最低的面孔。每个元组的第一个成员是脸的“SubjID”，而第二个成员是这个脸的完全不对称。列表必须按面部整体不对称的递增顺序排列，这样第一个元组将表示面部整体不对称程度最低的脸。因此，5元组序列将代表5个最低的面部不对称。

• OP4: The cosine similarity between faces F1 and F2 based on the six distances calculated above (OP2). The formula to calculate cosine similarity is provided at the end of this project sheet.

**OP4:**基于上面计算的6个距离的面F1和F2之间的余弦相似度(OP2)。计算余弦相似度的公式在项目表的最后提供。

All returned numeric outputs (in dictionaries, lists and individual values) must be rounded off to four decimal places (if required to be rounded off). Do not round off the values during calculations and round them off only while saving them into the final output variables.

Examples:

Download sampledata.csv file from the folder of Project 2 on LMS or Moodle. Some examples of how you can call your program from the Python shell (and examine the results it returns) are:

>>> [OP1,OP2,OP3,OP4]=main('SampleData.csv',['B7033','C1283'])

The outputs will be:

>>> OP1

[{'FT': 1.9198, 'EX': 1.8028, 'EN': 1.6555, 'AL': 2.5577, 'SBAL': 0.9023, 'CH': 1.7901},

{'FT': 1.807, 'EX': 2.2892, 'EN': 0.9371, 'AL': 1.9393, 'SBAL': 1.1624, 'CH': 2.7713}]

>>> OP2

[{'EXEN': 33.092, 'ENAL': 34.6946, 'ALEX': 50.1037, 'FTSBAL': 91.5324, 'SBALCH':

33.7109, 'CHFT': 98.1642}, {'EXEN': 34.4401, 'ENAL': 37.7494, 'ALEX': 54.0952,

'FTSBAL': 90.3202, 'SBALCH': 38.4123, 'CHFT': 104.8566}]

>>> OP3

[('E4996', 8.3254), ('H1178', 9.1597), ('F7831', 9.3268), ('J6687', 9.3878), ('K6431',

9.6359)]

>>> OP4

0.9991

There are few more requirements for your program.

• Your program needs to validate the inputs to the main() function and gracefully terminate if invalid inputs are provided.

• You program needs to terminate gracefully if the file cannot be found or opened. For graceful terminations, you need to print the message related to the problem and return None for each output.

• Your program needs to validate the input data from the file. The X, Y or Z coordinate (or all coordinates) of a landmark could be corrupted or missing. In that case the value in the cell would be empty or out of bounds. If data is not correct, then consider the entire data of this particular subject as corrupted. In such a case return ‘None’ for each unavailable output.

• It is possible that the Research Assistant marking these landmarks on the faces could have forgotten to mark/ record a landmark. In this case, that particular landmark will be missing from the CSV file. In such a case, consider the entire data of this particular subject as corrupted and return ‘None’ for each unavailable output.

• The facial asymmetry at nose tip must be zero. In case this value is non-zero, it means that the subject data has been corrupted and needs to be discarded. In such a case return ‘None’ for each unavailable output.

• Your program needs to consider that record of the landmarks for a particular Adult may not have any specific order or can be in any order (excluding header row).

• The columns in the CSV file can be in any order and the headings are case insensitive. The headings expected are: SubjID, Landmark, OX,O Y,0Z,MX,MY AND MZ.

CSV文件中的列可以是任意顺序的，标题不区分大小写。标题应该是:subject, Landmark, OX,O Y,0Z,MX,MY和MZ。

## # OP1

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# 公式：0.5 ** ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2)
with open(csvfile, "rt") as f:
return content_lst
# print(content_lst)

def parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""
OX_POSITION = 2
OY_POSITION = 3
OZ_POSITION = 4
MX_POSITION = 5
MY_POSITION = 6
MZ_POSITION = 7

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst[1:]:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
mx_value = detail_lst[MX_POSITION]
ox_value = detail_lst[OX_POSITION]
my_value = detail_lst[MY_POSITION]
oy_value = detail_lst[OY_POSITION]
mz_value = detail_lst[MZ_POSITION]
oz_value = detail_lst[OZ_POSITION]
# print("OX:", ox_value, "OY:", oy_value, "OZ:", oz_value, "MX:", mx_value, "MY:", my_value, "MZ:", mz_value)
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
else:
pass
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
new_result_dict = {}
for not_key in difference_set_keys:
result_dict[not_key] = "None"
sort_key = ['FT', 'EX', 'EN', 'AL', 'SBAL', 'CH']
for key in sort_key:
new_result_dict[key] = result_dict[key]
return new_result_dict
return result_dict
# print(set(result_dict))

def main(csvfile, SubjIDs: list):
# print(content_lst)
for subid in SubjIDs:
# print(subid)
r = parse(content_lst, subid)
print(r)
# print(content_lst)

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
main(path, ['B7033', 'C1283', 'I0951'])
``````

## # 规范 1

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# 公式：0.5 ** ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2)
try:
with open(csvfile, "rt") as f:
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""
OX_POSITION = 2
OY_POSITION = 3
OZ_POSITION = 4
MX_POSITION = 5
MY_POSITION = 6
MZ_POSITION = 7

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst[1:]:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
mx_value = detail_lst[MX_POSITION]
ox_value = detail_lst[OX_POSITION]
my_value = detail_lst[MY_POSITION]
oy_value = detail_lst[OY_POSITION]
mz_value = detail_lst[MZ_POSITION]
oz_value = detail_lst[OZ_POSITION]
# print("OX:", ox_value, "OY:", oy_value, "OZ:", oz_value, "MX:", mx_value, "MY:", my_value, "MZ:", mz_value)
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
else:
pass
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
new_result_dict = {}
for not_key in difference_set_keys:
result_dict[not_key] = "None"
sort_key = ['FT', 'EX', 'EN', 'AL', 'SBAL', 'CH']
for key in sort_key:
new_result_dict[key] = result_dict[key]
return new_result_dict
return result_dict
# print(set(result_dict))

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
if str(content_lst) != string:
for subid in SubjIDs:
# print(subid)
r = parse(content_lst, subid)
print(r)
else:
return None

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
main(path, ['B7033', 'C1283', 'I0951'])
``````
``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""
OX_POSITION = 2
OY_POSITION = 3
OZ_POSITION = 4
MX_POSITION = 5
MY_POSITION = 6
MZ_POSITION = 7

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst[1:]:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
mx_value = detail_lst[MX_POSITION]
ox_value = detail_lst[OX_POSITION]
my_value = detail_lst[MY_POSITION]
oy_value = detail_lst[OY_POSITION]
mz_value = detail_lst[MZ_POSITION]
oz_value = detail_lst[OZ_POSITION]
# print("OX:", ox_value, "OY:", oy_value, "OZ:", oz_value, "MX:", mx_value, "MY:", my_value, "MZ:", mz_value)
# if mx_value == "0" or my_value == "0" or mz_value == "0" or oz_value == "0" or ox_value == "0" or oy_value == "0":
if "0" in detail_lst:
result_dict[detail_lst[1].upper()] = "None"
else:
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
# new_result_dict = {}
# for not_key in difference_set_keys:
#     result_dict[not_key] = "None"
# sort_key = ['FT', 'EX', 'EN', 'AL', 'SBAL', 'CH']
# for key in sort_key:
#     new_result_dict[key] = result_dict[key]
# return new_result_dict
return None
return result_dict
# print(set(result_dict))

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
op1 = []
if str(content_lst) != string:
for subid in SubjIDs:
r = parse(content_lst, subid)
op1.append(r)
print(op1)
return [op1, None, None, None]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
main(path, ['B7033', 'C1283', 'I0951'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)

``````

## # op2

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""
OX_POSITION = 2
OY_POSITION = 3
OZ_POSITION = 4
MX_POSITION = 5
MY_POSITION = 6
MZ_POSITION = 7

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst[1:]:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
mx_value = detail_lst[MX_POSITION]
ox_value = detail_lst[OX_POSITION]
my_value = detail_lst[MY_POSITION]
oy_value = detail_lst[OY_POSITION]
mz_value = detail_lst[MZ_POSITION]
oz_value = detail_lst[OZ_POSITION]
# print("OX:", ox_value, "OY:", oy_value, "OZ:", oz_value, "MX:", mx_value, "MY:", my_value, "MZ:", mz_value)
# if mx_value == "0" or my_value == "0" or mz_value == "0" or oz_value == "0" or ox_value == "0" or oy_value == "0":
if "0" in detail_lst:
result_dict[detail_lst[1].upper()] = "None"
else:
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
# new_result_dict = {}
# for not_key in difference_set_keys:
#     result_dict[not_key] = "None"
# sort_key = ['FT', 'EX', 'EN', 'AL', 'SBAL', 'CH']
# for key in sort_key:
#     new_result_dict[key] = result_dict[key]
# return new_result_dict
return None
return result_dict
# print(set(result_dict))

def op2_parse(contet_lst, subid):
# keys_list = ["OX", ]
# print(contet_lst)

result_list = []
for detail_content in contet_lst[1:]:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
# keys = ["EXEN", "ENAI", "AIEX", "FTSBAL", "SBALCH", "CHFT"]
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
if str(content_lst) != string:
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# print(op1)
# ------------- OP Code -------------
return [op1, op2, None, None]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# main(path, ['B7033', 'C1283', 'I0951'])
main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)

``````
``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
# for detail_content in content_lst:
#     detail_content.replace("\n", "").split(",")
# return content_lst
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""
OX_POSITION = 2
OY_POSITION = 3
OZ_POSITION = 4
MX_POSITION = 5
MY_POSITION = 6
MZ_POSITION = 7

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
mx_value = detail_lst[MX_POSITION]
ox_value = detail_lst[OX_POSITION]
my_value = detail_lst[MY_POSITION]
oy_value = detail_lst[OY_POSITION]
mz_value = detail_lst[MZ_POSITION]
oz_value = detail_lst[OZ_POSITION]
# print("OX:", ox_value, "OY:", oy_value, "OZ:", oz_value, "MX:", mx_value, "MY:", my_value, "MZ:", mz_value)
# if mx_value == "0" or my_value == "0" or mz_value == "0" or oz_value == "0" or ox_value == "0" or oy_value == "0":
if "0" in detail_lst:
result_dict[detail_lst[1].upper()] = "None"
else:
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
# new_result_dict = {}
# for not_key in difference_set_keys:
#     result_dict[not_key] = "None"
# sort_key = ['FT', 'EX', 'EN', 'AL', 'SBAL', 'CH']
# for key in sort_key:
#     new_result_dict[key] = result_dict[key]
# return new_result_dict
return None
return result_dict
# print(set(result_dict))

def op2_parse(contet_lst, subid):
# keys_list = ["OX", ]
# print(contet_lst)

result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
# keys = ["EXEN", "ENAI", "AIEX", "FTSBAL", "SBALCH", "CHFT"]
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
if str(content_lst) != string:
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# print(op1)
# ------------- OP Code -------------
return [op1, op2, None, None]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# main(path, ['B7033', 'C1283', 'I0951'])
main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)

``````

## # op1 优化

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
# for detail_content in content_lst:
#     detail_content.replace("\n", "").split(",")
# return content_lst
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def enegine(detail_lst):
result_dict = {}
# mx_value = detail_lst[MX_POSITION]  # 5
# ox_value = detail_lst[OX_POSITION]  # 2
# my_value = detail_lst[MY_POSITION]  # 6
# oy_value = detail_lst[OY_POSITION]  # 3
# mz_value = detail_lst[MZ_POSITION]  # 7
# oz_value = detail_lst[OZ_POSITION]  # 4
# print(mx_value, ox_value, my_value, oy_value, mz_value, oz_value)
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
# print(mx_value, ox_value, my_value, oy_value, mz_value, oz_value)
# print("OX:", ox_value, "OY:", oy_value, "OZ:", oz_value, "MX:", mx_value, "MY:", my_value, "MZ:", mz_value)
# if mx_value == "0" or my_value == "0" or mz_value == "0" or oz_value == "0" or ox_value == "0" or oy_value == "0":
# if "0" in detail_lst:
#     result_dict[detail_lst[1].upper()] = "None"
# else:
#     result_dict[detail_lst[1].upper()] = (
#                                                  (float(mx_value) - float(ox_value)) ** 2 +
#                                                  (float(my_value) - float(oy_value)) ** 2 +
#                                                  (float(mz_value) - float(oz_value)) ** 2
#                                          ) ** 0.5
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
return result_dict
def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""
OX_POSITION = 2
OY_POSITION = 3
OZ_POSITION = 4
MX_POSITION = 5
MY_POSITION = 6
MZ_POSITION = 7

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
pass
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
# new_result_dict = {}
# for not_key in difference_set_keys:
#     result_dict[not_key] = "None"
# sort_key = ['FT', 'EX', 'EN', 'AL', 'SBAL', 'CH']
# for key in sort_key:
#     new_result_dict[key] = result_dict[key]
# return new_result_dict
return None
return result_dict
# print(set(result_dict))

def op2_parse(contet_lst, subid):
# keys_list = ["OX", ]
# print(contet_lst)

result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
# keys = ["EXEN", "ENAI", "AIEX", "FTSBAL", "SBALCH", "CHFT"]
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
if str(content_lst) != string:
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
print("op1:>>>>", op1)
# ------------- OP Code -------------
return [op1, op2, None, None]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# main(path, ['B7033', 'C1283', 'I0951'])
main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)

``````

## # op3

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
# for detail_content in content_lst:
#     detail_content.replace("\n", "").split(",")
# return content_lst
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
# print(dict(result_dict))
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
return None
return result_dict

def op2_parse(contet_lst, subid):
result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
# keys = ["EXEN", "ENAI", "AIEX", "FTSBAL", "SBALCH", "CHFT"]
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict

def op3_parse(content_lst):
keys_list = set()
result_list_data = []
for detail_content in content_lst:
result_dict = {}
# middle_lst = []
detail_lst = detail_content.replace("\n", "").split(",")
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict["ID"] = detail_lst[0]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5

result_list_data.append(result_dict)
# print(result_list_data)
result_list_sort = []
middle_dict = {}
for key in keys_list:
middle_list = []
for detail_d in result_list_data:
if key == detail_d["ID"]:
middle_list.append(list(detail_d.values())[1])
# print(f"key:>>>{key}", middle_list)
middle_dict[key] = sum(middle_list)
print(middle_dict)

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
if str(content_lst) != string:
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# op3
op3_parse(content_lst)
# print("op1:>>>>", op1)
# ------------- OP Code -------------
return [op1, op2, None, None]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# main(path, ['B7033', 'C1283', 'I0951'])
main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)

``````

### # OP3

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
# for detail_content in content_lst:
#     detail_content.replace("\n", "").split(",")
# return content_lst
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
# print(dict(result_dict))
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
return None
return result_dict

def op2_parse(contet_lst, subid):
result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
# keys = ["EXEN", "ENAI", "AIEX", "FTSBAL", "SBALCH", "CHFT"]
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict

def op3_parse(content_lst):
keys_list = set()
result_list_data = []
for detail_content in content_lst:
result_dict = {}
# middle_lst = []
detail_lst = detail_content.replace("\n", "").split(",")
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict["ID"] = detail_lst[0]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5

result_list_data.append(result_dict)
# print(result_list_data)
result_list_sort = []
middle_dict = {}
for key in keys_list:
middle_list = []
for detail_d in result_list_data:
if key == detail_d["ID"]:
middle_list.append(list(detail_d.values())[1])
# print(f"key:>>>{key}", middle_list)
middle_dict[key] = sum(middle_list)
# print(middle_dict)
sorted_by_value = sorted(middle_dict.items(), key=lambda x: x[1])[:5]
# print(sorted_by_value)
return sorted_by_value

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
if str(content_lst) != string:
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# op3
op3 = op3_parse(content_lst)
# print("op1:>>>>", op1)
# ------------- OP Code -------------
return [op1, op2, op3, None]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# main(path, ['B7033', 'C1283', 'I0951'])
main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)
``````

## # OP4

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
# for detail_content in content_lst:
#     detail_content.replace("\n", "").split(",")
# return content_lst
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
# print(dict(result_dict))
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
return None
return result_dict

def op2_parse(contet_lst, subid):
result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
# keys = ["EXEN", "ENAI", "AIEX", "FTSBAL", "SBALCH", "CHFT"]
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict

def op3_parse(content_lst):
keys_list = set()
result_list_data = []
for detail_content in content_lst:
result_dict = {}
# middle_lst = []
detail_lst = detail_content.replace("\n", "").split(",")
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict["ID"] = detail_lst[0]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5

result_list_data.append(result_dict)
# print(result_list_data)
result_list_sort = []
middle_dict = {}
for key in keys_list:
middle_list = []
for detail_d in result_list_data:
if key == detail_d["ID"]:
middle_list.append(list(detail_d.values())[1])
# print(f"key:>>>{key}", middle_list)
middle_dict[key] = sum(middle_list)
# print(middle_dict)
sorted_by_value = sorted(middle_dict.items(), key=lambda x: x[1])[:5]
# print(sorted_by_value)
return sorted_by_value

def op4_parse(op2):
# print(op2)
v1 = list(op2[0].values())
v2 = list(op2[1].values())
total = 0
total_v1 = 0
total_v2 = 0
for v1, v2 in zip(v1, v2):
# print(v1, v2)
total += v1 * v2
total_v1 += v1 ** 2
total_v2 += v2 ** 2
# print(total)
r = round(total / ((total_v1 ** 0.5) * (total_v2 ** 0.5)), 4)
# print(r)
return r

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
if str(content_lst) != string:
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# op3
op3 = op3_parse(content_lst)
op4 = op4_parse(op2)
# print("op1:>>>>", op1)
# ------------- OP Code -------------
return [op1, op2, op3, op4]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# main(path, ['B7033', 'C1283', 'I0951'])
main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)
``````
``````result_dict[detail_lst[1].upper()] = float("%.4f" % value)
``````
``````value = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5

result_dict[detail_lst[1].upper()] = float("%.4f" % value)
``````
``````middle_dict[key] = round(sum(middle_list), 4)
``````

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5

try:
with open(csvfile, "rt") as f:
# for detail_content in content_lst:
#     detail_content.replace("\n", "").split(",")
# return content_lst
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
# print(dict(result_dict))
if result_dict["PRN"] != 0:
# result_dict["PRN"] = "None"
# result_dict = "None"
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
return None
return result_dict

def op2_parse(contet_lst, subid):
result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
# keys = ["EXEN", "ENAI", "AIEX", "FTSBAL", "SBALCH", "CHFT"]
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict

def op3_parse(content_lst):
keys_list = set()
result_list_data = []
for detail_content in content_lst:
result_dict = {}
# middle_lst = []
detail_lst = detail_content.replace("\n", "").split(",")
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict["ID"] = detail_lst[0]
value = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5

result_dict[detail_lst[1].upper()] = float("%.4f" % value)
# print(result_dict)
result_list_data.append(result_dict)
# print(result_list_data)
# result_list_sort = []
middle_dict = {}
for key in keys_list:
middle_list = []
for detail_d in result_list_data:
if key == detail_d["ID"]:
middle_list.append(list(detail_d.values())[1])
# print(f"key:>>>{key}", middle_list)
middle_dict[key] = round(sum(middle_list), 4)
# print(middle_dict)
sorted_by_value = sorted(middle_dict.items(), key=lambda x: x[1])[:5]
# print(sorted_by_value)
return sorted_by_value

def op4_parse(op2):
# print(op2)
v1 = list(op2[0].values())
v2 = list(op2[1].values())
total = 0
total_v1 = 0
total_v2 = 0
for v1, v2 in zip(v1, v2):
# print(v1, v2)
total += v1 * v2
total_v1 += v1 ** 2
total_v2 += v2 ** 2
# print(total)
r = round(total / ((total_v1 ** 0.5) * (total_v2 ** 0.5)), 4)
# print(r)
return r

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
if str(content_lst) != string:
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# op3
op3 = op3_parse(content_lst)
print(op3)
op4 = op4_parse(op2)
# print("op1:>>>>", op1)
# ------------- OP Code -------------
return [op1, op2, op3, op4]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# main(path, ['B7033', 'C1283', 'I0951'])
main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)
``````

``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5
NONE_START = False
KEYS = set()

global KEYS
try:
with open(csvfile, "rt") as f:
for text in content_lst:
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
# print(dict(result_dict))
if result_dict["PRN"] != 0:
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
global NONE_START
NONE_START = True
return None
return result_dict

def op2_parse(contet_lst, subid):
result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
if (set(judge_dict) & judge_set_keys) == judge_set_keys:
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict
else:
return None

def op3_parse(content_lst):
keys_list = set()
result_list_data = []
for detail_content in content_lst:
result_dict = {}
# middle_lst = []
detail_lst = detail_content.replace("\n", "").split(",")
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict["ID"] = detail_lst[0]
value = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5

result_dict[detail_lst[1].upper()] = float("%.4f" % value)
# print(result_dict)
result_list_data.append(result_dict)
# print(result_list_data)
# result_list_sort = []
middle_dict = {}
for key in keys_list:
middle_list = []
for detail_d in result_list_data:
if key == detail_d["ID"]:
middle_list.append(list(detail_d.values())[1])
# print(f"key:>>>{key}", middle_list)
middle_dict[key] = round(sum(middle_list), 4)
# print(middle_dict)
sorted_by_value = sorted(middle_dict.items(), key=lambda x: x[1])[:5]
# print(sorted_by_value)
return sorted_by_value

def op4_parse(op2):
# print("ssss", op2)
if None in op2:
return None
v1 = list(op2[0].values())
v2 = list(op2[1].values())
total = 0
total_v1 = 0
total_v2 = 0
for v1, v2 in zip(v1, v2):
# print(v1, v2)
total += v1 * v2
total_v1 += v1 ** 2
total_v2 += v2 ** 2
# print(total)
r = round(total / ((total_v1 ** 0.5) * (total_v2 ** 0.5)), 4)
# print(r)
return r

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
for id in SubjIDs:
if id not in KEYS:
return [None, None, None, None]
if str(content_lst) != string:
# print(content_lst)
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# op3
op3 = op3_parse(content_lst)
# print(op3)
op4 = op4_parse(op2)
# print("op1:>>>>", op1)
# ------------- OP Code -------------
# if NONE_START:
#     return [None, None, None, None]
# else:
return [op1, op2, op3, op4]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# op1, op2, op3, op4 = main(path, ['B7033', 'C1283', 'I0951'])
op1, op2, op3, op4 = main(path, ['I0951', 'I0951'])
# op1, op2, op3, op4 = main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# print(op1, op2, op3, op4)
print(op1)
print(op2)
print(op3)
print(op4)

# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)

``````
``````# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 12:59
# @Author  : AI悦创
# @FileName: OP1.py
# @Software: PyCharm
# OP1@Blog    ：https://bornforthis.cn/
# op1 公式1： ((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
# op2 公式2： ((En)) ** 0.5
NONE_START = False
KEYS = set()

global KEYS
try:
with open(csvfile, "rt") as f:
for text in content_lst:
return content_lst
# print(content_lst)
except FileNotFoundError as e:
# print(e)
return e

def op1_parse(content_lst, subid):
"""
((MX - OX)^2 + (MY - OY)^2 + (MZ - OZ)^2) ** 0.5
:param content_lst:
:return: op1
"""

judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
result_dict = {}
for detail_content in content_lst:
detail_lst = detail_content.replace("\n", "").split(",")
# print(detail_lst)
if subid in detail_lst:
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict[detail_lst[1].upper()] = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5
# print(dict(result_dict))
if result_dict["PRN"] != 0:
return None
else:
del result_dict["PRN"]
difference_set_keys = judge_set_keys - set(result_dict)
if difference_set_keys:
global NONE_START
NONE_START = True
return None
return result_dict

def op2_parse(contet_lst, subid):
result_list = []
for detail_content in contet_lst:
# print(detail_content)
content_lst = detail_content.replace("\n", "").split(",")
result_list.append(content_lst)

data_list = []
operation_lst = []
for content in result_list:
# print(content)
if subid == content[0]:
data_list.append(("SubID", content[0]))
data_list.append((content[1].upper(), content[2:]))
judge_dict = dict(data_list)
# print(judge_dict)
judge_set_keys = {'FT', 'CH', 'AL', 'EX', 'EN', 'SBAL'}
if (set(judge_dict) & judge_set_keys) == judge_set_keys:
result_dict = {}
EN_LIST = list(map(float, judge_dict["EN"]))[:3]
EX_LIST = list(map(float, judge_dict["EX"]))[:3]
AL_LIST = list(map(float, judge_dict["AL"]))[:3]
FT_LIST = list(map(float, judge_dict["FT"]))[:3]
SBAL_LIST = list(map(float, judge_dict["SBAL"]))[:3]
CH_LIST = list(map(float, judge_dict["CH"]))[:3]
f_lambda = lambda x: (x[0] - x[1]) ** 2
result_dict["EXEN"] = sum([f_lambda(detail) for detail in zip(EN_LIST, EX_LIST)]) ** 0.5
result_dict["ENAL"] = sum([f_lambda(detail) for detail in zip(AL_LIST, EN_LIST)]) ** 0.5
result_dict["ALEX"] = sum([f_lambda(detail) for detail in zip(EX_LIST, AL_LIST)]) ** 0.5
result_dict["FTSBAL"] = sum([f_lambda(detail) for detail in zip(SBAL_LIST, FT_LIST)]) ** 0.5
result_dict["SBALCH"] = sum([f_lambda(detail) for detail in zip(CH_LIST, SBAL_LIST)]) ** 0.5
result_dict["CHFT"] = sum([f_lambda(detail) for detail in zip(FT_LIST, CH_LIST)]) ** 0.5
# print(result_dict)
return result_dict
else:
return None

def op3_parse(content_lst):
keys_list = set()
result_list_data = []
for detail_content in content_lst:
result_dict = {}
# middle_lst = []
detail_lst = detail_content.replace("\n", "").split(",")
ox_value, oy_value, oz_value, mx_value, my_value, mz_value = detail_lst[2:]
result_dict["ID"] = detail_lst[0]
value = (
(float(mx_value) - float(ox_value)) ** 2 +
(float(my_value) - float(oy_value)) ** 2 +
(float(mz_value) - float(oz_value)) ** 2
) ** 0.5

result_dict[detail_lst[1].upper()] = float("%.4f" % value)
# print(result_dict)
result_list_data.append(result_dict)
# print(result_list_data)
# result_list_sort = []
middle_dict = {}
for key in keys_list:
middle_list = []
for detail_d in result_list_data:
if key == detail_d["ID"]:
middle_list.append(list(detail_d.values())[1])
# print(f"key:>>>{key}", middle_list)
middle_dict[key] = round(sum(middle_list), 4)
# print(middle_dict)
sorted_by_value = sorted(middle_dict.items(), key=lambda x: x[1])[:5]
# print(sorted_by_value)
return sorted_by_value

def op4_parse(op2):
# print("ssss", op2)
if None in op2:
return None
v1 = list(op2[0].values())
v2 = list(op2[1].values())
total = 0
total_v1 = 0
total_v2 = 0
for v1, v2 in zip(v1, v2):
# print(v1, v2)
total += v1 * v2
total_v1 += v1 ** 2
total_v2 += v2 ** 2
# print(total)
r = round(total / ((total_v1 ** 0.5) * (total_v2 ** 0.5)), 4)
# print(r)
return r

def main(csvfile, SubjIDs: list):
# print(content_lst)
string = "[Errno 2] No such file or directory: '{path}'".format(path=csvfile)
# print(string)
# print(str(content_lst) != string)
# ------------- OP Code -------------
for id in SubjIDs:
if id not in KEYS:
return [None, None, None, None]
if str(content_lst) != string:
# print(content_lst)
op1 = []
op2 = []
for subid in SubjIDs:
# op1
r = op1_parse(content_lst, subid)
op1.append(r)
# op2
r = op2_parse(content_lst, subid)
op2.append(r)
# op3
op3 = op3_parse(content_lst)
# print(op3)
op4 = op4_parse(op2)
# print("op1:>>>>", op1)
# ------------- OP Code -------------
# if NONE_START:
#     return [None, None, None, None]
# else:
return [op1, op2, op3, op4]
else:
return [None, None, None, None]

def title(path):
r = []
for d in content_lst[1:]:
n_d = d.split(",")[0]
r.append(n_d)
# print(r)
return r

if __name__ == '__main__':
path = "data/SampleData.csv"  # 路径
# op1, op2, op3, op4 = main(path, ['B7033', 'C1283', 'I0951'])
# op1, op2, op3, op4 = main(path, ['I0951', 'I0951'])
op1, op2, op3, op4 = main(path, ['B7033', 'C1283'])
# op1, op2, op3, op4 = main(path, ['B7033', 'C1283'])
# main(path, ['B7033'])
# print(op1, op2, op3, op4)
print(op1)
print(op2)
print(op3)
print(op4)

# lst = title(path)
# main(path, ['B7033', 'C1283'])
# main(path, lst)

``````

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度