Define a function sort_list(list1) that returns the original list, the list in ascending order, and the list in descending order.
时间: 2024-02-15 22:02:21 浏览: 37
Sure, here's the code for the function you requested:
```python
def sort_list(list1):
sorted_list = sorted(list1)
reverse_sorted_list = sorted(list1, reverse=True)
return list1, sorted_list, reverse_sorted_list
```
This function takes a list as the input argument and uses the `sorted()` function to generate a new list that is sorted in ascending order. It then uses the same function with the `reverse` parameter set to `True` to generate a new list that is sorted in descending order. Finally, it returns a tuple containing the original list, the sorted list, and the reverse sorted list.
相关问题
Define a function first_last(list1) that returns a list with the first and last element of list1 interchanged.
Here's the Python code for the function `first_last(list1)` that returns a list with the first and last element of `list1` interchanged:
```python
def first_last(list1):
if len(list1) < 2:
return list1
else:
first = list1[0]
last = list1[-1]
list1[0] = last
list1[-1] = first
return list1
```
This function takes in a list `list1` as its input and first checks if the length of the list is less than 2. If the length is less than 2, it simply returns the original list. Otherwise, it stores the first element of the list in a variable called `first` and the last element of the list in a variable called `last`. It then interchanges the values of `first` and `last` in the list and returns the modified list.
Sort the student list in the ascending order of student names and print them all on the screen
Sure, here's the updated code to sort the students by name in ascending order and print them to the screen:
```
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
// Read students from file
try {
Scanner scanner = new Scanner(new File("students.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
String name = parts[0];
ArrayList<String> hobbies = new ArrayList<String>();
for (int i = 1; i < parts.length; i++) {
hobbies.add(parts[i]);
}
Student student = new Student(name, hobbies);
students.add(student);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
// Sort students by name in ascending order
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
// Print sorted list of students
for (Student student : students) {
System.out.println(student.getName() + ": " + student.getHobbies());
}
}
}
```
This code uses the Collections.sort() method to sort the ArrayList of students by name in ascending order. It also uses an anonymous inner class that implements the Comparator interface to define the sorting order. Finally, it loops through the sorted list of students and prints each student's name and hobbies to the console.