list1 = ["a","a","a","b","b","c","d","d","f"]
list1 = list(set(list1)) # 先将list1转化为set集合去重,再将集合转化为列表
# 方法二:定义临时空列表,遍历原始列表,保存不重复的元素,最后打印临时列表。
list2 = ["a","a","a","b","b","c","d","d","f"]
temp_list = [] # 定义一个临时空列表,用于保存临时数据。
for i in list2: # 遍历原列表,判断如果元素不在临时列表,就追加进去,如果在,就不加。
# 方法三:双重循环,外层循环遍历列表,内存循环控制删除重复元素的次数。如果元素个数统计大于1,则执行删除。
list3 = ["a","a","a","b","b","c","d","d","f"]
list3.reverse() # 先反转列表,从后往前删
for i in list3: # 外层循环控制每次需要判定的元素
for _ in range(list3.count(i)): # 内层循环控制每次执行删除元素的次数,循环多次就彻底删除。
list3.remove(i) # 如果该元素个数大于1,则执行删除操作
list3.reverse() # 最后再反转列表,保证第一次出现的元素顺位保留下来