‘’’
写出一段Python代码实现删除一个list里面的重复元素
‘’’
list = ['hello', 'good', 'world', 'hello', 'kitty', 'kitty', 'hello']
new_list = []
for word in list:
if word not in new_list:
new_list.append(word)
print(new_list)
import random
li = []
for i in range(10):
ran = random.randint(1,10)
li.append(ran)
print(li)
for i in li:
if li.count(i) >= 2:
li.remove(i)
print(li)
本文介绍了一种使用Python去除列表中重复元素的方法,包括字符串列表和随机整数列表的去重。通过遍历列表并检查元素是否已存在于新列表中来实现,确保了列表元素的唯一性。
569

被折叠的 条评论
为什么被折叠?



