用Python实现一个Johnson变换的代码,输入一个列表list_x,输出转化后的列表list_y。
时间: 2023-03-08 15:52:43 浏览: 190
def Johnson_transform(list_x):
list_y = []
for i in range(len(list_x)):
list_y.append((list_x[i] + np.sqrt(list_x[i]**2 + 4))/2)
return list_y
相关问题
python读取list写入csv
可以使用Python内置的csv模块来将列表写入CSV文件。下面是一个示例代码:
```python
import csv
# 一个包含数据的列表
data = [
['John', 'Doe', '123 Main St.'],
['Jane', 'Smith', '456 Broadway Ave.'],
['Bob', 'Johnson', '789 Market St.']
]
# 打开CSV文件进行写入
with open('output.csv', mode='w', newline='') as file:
# 创建一个CSV写入器
writer = csv.writer(file)
# 将数据写入CSV文件
writer.writerows(data)
```
这将会创建一个名为`output.csv`的文件,并将列表`data`写入其中。请注意,CSV写入器的`writerows`方法可以一次性写入多行数据。如果需要写入单行数据,可以使用`writer.writerow()`方法。
如何用python实现简单的学生选课系统
以下是一个简单的学生选课系统的 Python 代码示例:
```python
class Student:
def __init__(self, name, id_num):
self.name = name
self.id_num = id_num
self.courses = []
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
self.courses.remove(course)
class Course:
def __init__(self, name, id_num, professor):
self.name = name
self.id_num = id_num
self.professor = professor
self.students = []
def add_student(self, student):
self.students.append(student)
student.add_course(self)
def remove_student(self, student):
self.students.remove(student)
student.remove_course(self)
def list_students(self):
for student in self.students:
print(student.name)
class School:
def __init__(self):
self.students = []
self.courses = []
def add_student(self, name, id_num):
student = Student(name, id_num)
self.students.append(student)
def add_course(self, name, id_num, professor):
course = Course(name, id_num, professor)
self.courses.append(course)
def register_student(self, student_id, course_id):
student = self.get_student_by_id(student_id)
course = self.get_course_by_id(course_id)
course.add_student(student)
def unregister_student(self, student_id, course_id):
student = self.get_student_by_id(student_id)
course = self.get_course_by_id(course_id)
course.remove_student(student)
def list_students_in_course(self, course_id):
course = self.get_course_by_id(course_id)
course.list_students()
def get_student_by_id(self, student_id):
for student in self.students:
if student.id_num == student_id:
return student
return None
def get_course_by_id(self, course_id):
for course in self.courses:
if course.id_num == course_id:
return course
return None
# Example usage
school = School()
school.add_student("Alice", 1234)
school.add_student("Bob", 5678)
school.add_course("Math", 1001, "Prof. Smith")
school.add_course("Science", 1002, "Prof. Johnson")
school.register_student(1234, 1001)
school.register_student(5678, 1001)
school.register_student(5678, 1002)
school.list_students_in_course(1001)
```
这个简单的学生选课系统包括三个类:`Student`,`Course`和`School`。`Student`类表示学生,`Course`类表示课程,`School`类表示学校。在`School`类中,有一些方法可以添加学生和课程,注册学生到课程中,取消学生在课程中的注册,列出某个课程中的所有学生等。
阅读全文