编写搩搫,实现删除列表搪搨元素的功能。 如输入 11231234123456 输出为[1,2,3,4,5,6]
时间: 2024-05-25 15:18:36 浏览: 6
def remove_duplicates(lst):
"""
删除列表中的重复元素,返回一个不含重复元素的新列表
"""
new_lst = []
for item in lst:
if item not in new_lst:
new_lst.append(item)
return new_lst
# 测试代码
if __name__ == '__main__':
lst = [1, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6]
new_lst = remove_duplicates(lst)
print(new_lst) # 打印结果为 [1, 2, 3, 4, 5, 6]
相关问题
编写函数,统计某个元素在列表中出现的次数。实现输入:x=5,输出:3次
好的,可以使用 Python 的内置函数 `count` 来实现。以下是一个示例函数:
```python
def count_element(lst, element):
return lst.count(element)
lst = [5, 2, 3, 5, 4, 5, 1]
x = 5
count = count_element(lst, x)
print(f"{x}出现的次数为:{count}次")
```
其中,`lst` 是列表,`element` 是要统计出现次数的元素。函数返回该元素在列表中出现的次数。在这个例子里,我们定义了一个列表`lst`,并且元素`5`在这个列表中出现了3次,所以输出结果为`5出现的次数为:3次`。
测试输入: 无 测试输出: 取出元素 3 取出元素 1 取出元素 4 取出元素 8 取出元素 6编写代码
好的,根据你的要求,我可以为你编写一段 Python 代码来实现这个测试用例的功能。下面是代码:
```
list = [3, 1, 4, 8, 6]
for i in range(len(list)):
print("取出元素", list[i])
```
这段代码首先创建了一个包含 5 个元素的列表,然后使用 for 循环遍历这个列表,并依次取出每个元素并打印输出。输出结果与你提供的测试输出是一致的。