现有一列表 ls = ['the lord of the rings','anaconda','legally blonde','gone with the wind'],编写程序,实现以下功能:
时间: 2023-05-31 15:21:06 浏览: 129
### 回答1:
1. 将列表按照字母顺序排序并输出。
2. 在列表末尾添加一部新电影'avatar',并输出新列表。
3. 输出列表中第3部电影的名称。
4. 将列表中第2部电影的名称改为'titanic',并输出新列表。
5. 删除列表中第1部电影的名称,输出新列表。
6. 输出列表中电影的总数。
7. 将列表转换为元组,并输出。
程序如下:
```python
ls = ['the lord of the rings','anaconda','legally blonde','gone with the wind']
# 1. 将列表按照字母顺序排序并输出。
ls.sort()
print(ls)
# 2. 在列表末尾添加一部新电影'avatar',并输出新列表。
ls.append('avatar')
print(ls)
# 3. 输出列表中第3部电影的名称。
print(ls[2])
# 4. 将列表中第2部电影的名称改为'titanic',并输出新列表。
ls[1] = 'titanic'
print(ls)
# 5. 删除列表中第1部电影的名称,输出新列表。
del ls[0]
print(ls)
# 6. 输出列表中电影的总数。
print(len(ls))
# 7. 将列表转换为元组,并输出。
tup = tuple(ls)
print(tup)
```
输出结果如下:
```
['anaconda', 'gone with the wind', 'legally blonde', 'the lord of the rings']
['anaconda', 'gone with the wind', 'legally blonde', 'the lord of the rings', 'avatar']
legally blonde
['anaconda', 'titanic', 'legally blonde', 'the lord of the rings', 'avatar']
['titanic', 'legally blonde', 'the lord of the rings', 'avatar']
4
('titanic', 'legally blonde', 'the lord of the rings', 'avatar')
```
### 回答2:
题目要求实现以下功能:
- 输出列表 ls 中所有电影的名字,且首字母大写(title case)。
- 添加电影名字 "titanic" 到列表 ls 中。
- 从列表 ls 中删除电影名字 "anaconda"。
- 根据用户输入的电影名字,判断该电影名字是否在列表 ls 中。
为了实现以上功能,我们可以分别使用 Python 的内置函数和列表方法。针对每一个功能,我们可以采用以下方法实现:
1. 输出列表中所有电影名字
在 Python 中,我们可以使用 for 循环遍历列表中的每个元素,并利用字符串的 title() 方法将其首字母转为大写。具体实现:
```
for movie in ls:
print(movie.title())
```
2. 添加电影名字 "titanic" 到列表 ls 中
对于添加元素至列表的操作,我们可以使用 append() 方法。具体实现:
```
ls.append('titanic')
```
3. 从列表 ls 中删除电影名字 "anaconda"
对于删除元素的操作,我们可以使用 remove() 方法。具体实现:
```
ls.remove('anaconda')
```
4. 根据用户输入的电影名字,判断该电影名字是否在列表 ls 中
在判断某个元素是否在列表中时,我们可以使用 in 运算符。具体实现:
```
movie_name = input('请输入电影名字:')
if movie_name in ls:
print('该电影在列表中')
else:
print('该电影不在列表中')
```
实现以上代码后,我们就可以方便地实现题目所要求的功能啦!
### 回答3:
要求用户输入一个字符串作为搜索关键字,并在现有列表中查找包含该关键字的电影名称。如果找到电影名称,则将其打印在屏幕上,否则显示未找到该电影。
首先,我们需要编写一个函数来处理用户输入。该函数应该提示用户输入一个字符串,并返回该字符串。
```python
def get_input():
return input("请输入搜索关键字:")
```
接下来,我们需要编写一个函数来查找电影名称。该函数应该接受一个关键字,遍历现有列表中的每个电影名称,并返回包含该关键字的电影名称。如果没有找到任何电影名称,则返回 None。
```python
def search_movies(keyword):
for movie in ls:
if keyword.lower() in movie.lower():
return movie
return None
```
最后,我们需要编写一个主函数来调用其他两个函数,并显示搜索结果。
```python
def main():
keyword = get_input()
movie = search_movies(keyword)
if movie:
print("找到电影:", movie)
else:
print("未找到该电影。")
if __name__ == "__main__":
main()
```
这样,我们就可以输入一个关键字,程序将扫描现有列表并返回包含该关键字的电影名称。这种方法可以轻松地扩展到更大的电影数据库,只需简单地修改现有列表即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)