Define a function get_title(movielist) that returns a new list of movies with (year) removed. movielist contains titles of movie as string and year is always written as 4 digits within parathesis at the end of the title. For example 'Transformers (2007)' Example get_title(['Transformers (2007)']) -> ['Transformers'] movies1 = ['Transformers (2007)', 'Star Trek (2009)', "Schindler's List (1993)", 'Ip Man (2008)', 'Casino Royale (2006)', 'Back to the Future (1985)', 'Moon (2009)'] get_title(movies1) -> ['Transformers', 'Star Trek', "Schindler's List", 'Ip Man', 'Casino Royale', 'Back to the Future', 'Moon']
时间: 2023-06-05 13:07:13 浏览: 72
Sure, I can help you with that! Here's the function to remove the year from each movie title:
```python
def get_title(movielist):
new_list = []
for movie in movielist:
new_title = movie.split(' (')[0]
new_list.append(new_title)
return new_list
```
You can use it like this:
```python
movies1 = ['Transformers (2007)', 'Star Trek (2009)', "Schindler's List (1993)", 'Ip Man (2008)', 'Casino Royale (2006)', 'Back to the Future (1985)', 'Moon (2009)']
print(get_title(movies1))
```
This will output:
```
['Transformers', 'Star Trek', "Schindler's List", 'Ip Man', 'Casino Royale', 'Back to the Future', 'Moon']
```
相关问题
Define a function get_title(movielist) that returns a new list of movies with (year) removed. movielist contains titles of movie as string and year is always written as 4 digits within parathesis at the end of the title. For example 'Transformers (2007)
Sure, here's the Python code for the `get_title` function:
```python
import re
def get_title(movielist):
"""
Removes the year from the titles in movielist and returns a new list of titles.
Args:
- movielist: a list of movie titles as strings
Returns:
- a new list of movie titles with the year removed
"""
# Use regex to match the year pattern at the end of each title
pattern = r'\s*\([\d]{4}\)$'
# Create a new list of titles with the year removed
new_titles = []
for title in movielist:
new_title = re.sub(pattern, '', title)
new_titles.append(new_title)
return new_titles
```
The function first defines a regular expression pattern to match the year at the end of each title. It then creates a new list of titles with the year removed using a for loop and the `re.sub()` function to replace the year pattern with an empty string. Finally, the function returns the new list of titles.
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.