第15824题 程序题
Python列表去重三种实现方法代码补全

题目要求

对于给定的列表,要求对列表中每个重复元素只输出一次,请补全以下3种去重算法的代码。 测试用例列表:a = [11, 1, 14, 23, 11, 89, 14, 56, 89]

# 方法1
a = [11, 1, 14, 23, 11, 89, 14, 56, 89]
result = []
for i in a:
    if i not in result:
        ①
print("方法1:", result)

# 方法2
a = [11, 1, 14, 23, 11, 89, 14, 56, 89]
for i in [j for j in a if a.count(j) > 1]:
    for x in range(a.count(i) - 1):
        ②
print("方法2:", a)

# 方法3
a = [11, 1, 14, 23, 11, 89, 14, 56, 89]
i = 0
while i <= len(a) - 1:
    if a.count(a[i]) > 1:
        ③
    else:
        ④
print("方法3:", a)
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
编辑模式
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析