(1)删除列表fruits_1中的元素15,并返回删除元素; (2)判断'peach'元素是否存在于fruits_1中; (3)将列表fruits_1进行逆序存放; (4)利用列表fruits_1中字符串元素创建新的列表fruit_2,分别对fruit_2进行降序排列和按照字母长度进行升序排列; (5)利用for循环在一行中输出列表fruits_1的所有元素; (6)利用列表推导式输出列表fruits_1中的所有元素; (7)输出列表fruits_1的长度和元素'peach'出现的次数; (8)删除列表fruits_1中所有的'peach'元素。
时间: 2023-05-31 08:02:38 浏览: 130
(1)
fruits_1 = [10, 15, 20, 25, 30]
deleted_element = fruits_1.pop(1)
print("删除的元素为:", deleted_element)
print("删除后的列表为:", fruits_1)
(2)
fruits_1 = ['apple', 'banana', 'peach', 'orange']
if 'peach' in fruits_1:
print("元素'peach'存在于列表fruits_1中")
else:
print("元素'peach'不存在于列表fruits_1中")
(3)
fruits_1 = ['apple', 'banana', 'peach', 'orange']
fruits_1.reverse()
print("逆序后的列表为:", fruits_1)
(4)
fruits_1 = ['apple', 'banana', 'peach', 'orange']
fruit_2 = sorted(fruits_1, reverse=True)
print("降序排列后的fruit_2为:", fruit_2)
fruit_2 = sorted(fruits_1, key=len)
print("按照字母长度升序排列后的fruit_2为:", fruit_2)
(5)
fruits_1 = ['apple', 'banana', 'peach', 'orange']
for fruit in fruits_1:
print(fruit, end=" ")
(6)
fruits_1 = ['apple', 'banana', 'peach', 'orange']
fruit_list = [fruit for fruit in fruits_1]
print("输出的列表为:", fruit_list)
(7)
fruits_1 = ['apple', 'banana', 'peach', 'orange', 'peach']
print("列表fruits_1的长度为:", len(fruits_1))
print("元素'peach'在列表fruits_1中出现的次数为:", fruits_1.count('peach'))
(8)
fruits_1 = ['apple', 'banana', 'peach', 'orange', 'peach']
for fruit in fruits_1:
if fruit == 'peach':
fruits_1.remove(fruit)
print("删除'peach'元素后的列表为:", fruits_1)
阅读全文