2..已知列表 no_primes = [4, 6, 6, 8, 8, 9, 10, 10, 12, 12, 12, 12, 14, 14, 15, 15, 16, 16, 16], 请删除列表中重复的元素
时间: 2023-08-29 19:07:59 浏览: 68
可以使用集合(set)来去除重复元素,然后将其转换回列表:
```python
no_primes = [4, 6, 6, 8, 8, 9, 10, 10, 12, 12, 12, 12, 14, 14, 15, 15, 16, 16, 16]
no_primes = list(set(no_primes))
print(no_primes)
```
输出结果为:
```
[4, 6, 8, 9, 10, 12, 14, 15, 16]
```
相关问题
# 2..已知列表 # no_primes = [4, 6, 6, 8, 8, 9, 10, 10, 12, 12, 12, 12, 14, 14, 15, 15, 16, 16, 16], # 请删除列表中重复的元素 #
以下是删除列表中重复元素的代码:
```python
no_primes = [4, 6, 6, 8, 8, 9, 10, 10, 12, 12, 12, 12, 14, 14, 15, 15, 16, 16, 16]
no_primes = list(set(no_primes))
print(no_primes)
```
输出结果为:
```
[4, 6, 8, 9, 10, 12, 14, 15, 16]
```
利用 `set` 函数可以将列表中重复的元素去掉,然后再转为 `list` 即可。
python已知列表 no_primes = [4, 6, 6, 8, 8, 9, 10, 10, 12, 12, 12, 12, 14, 14, 15, 15, 16, 16, 16], 请删除列表中重复的元素
可以使用 set() 函数将列表转换成集合,因为集合中不允许有重复元素,然后再将集合转换成列表即可。代码如下:
```python
no_primes = [4, 6, 6, 8, 8, 9, 10, 10, 12, 12, 12, 12, 14, 14, 15, 15, 16, 16, 16]
no_primes = list(set(no_primes))
print(no_primes)
```
输出结果为:
```
[4, 6, 8, 9, 10, 12, 14, 15, 16]
```
阅读全文