1,给定一个列表:将列表中指定的某个元素全部删除:, 2,自定义一个数字列表,求列表中第二大数的下标; 3,列表去重, 将下面的列表中重复的元素去除, 不能使用set() ;
时间: 2023-05-23 15:05:20 浏览: 464
删除数组中的指定下标的元素
A:
1. 代码示例:
```python
lst = [1, 2, 3, 2, 4, 2]
to_remove = 2
while to_remove in lst:
lst.remove(to_remove)
print(lst) # [1, 3, 4]
```
2. 代码示例:
```python
lst = [4, 2, 7, 1, 5, 3]
max1 = max(lst)
max2 = float('-inf')
for num in lst:
if num != max1 and num > max2:
max2 = num
if max2 in lst:
index = lst.index(max2)
print(index) # 输出第二大数的下标
```
3. 代码示例:
```python
lst = [1, 2, 3, 2, 4, 2]
new_lst = []
for num in lst:
if num not in new_lst:
new_lst.append(num)
print(new_lst) # [1, 2, 3, 4]
```
阅读全文