Python程序从列表中删除重复元素?
一个列表具有重复元素,我们的任务是创建另一个列表,其中包含没有重复元素。
示例
A::[2,3,4,3,4,6,78,90] Output::[2,3,4,6,78,90]
算法
Step 1: create a list. Step 2: create a new list which is empty. Step 3: traverse every element in list. Step 4: if element is not present in the list return true. Step 5: append in the new list. Step 6: display new list.
范例程式码
# To remove duplicate elements
defremoveduplicateele(A):
newlist = []
for n in A:
if n not in newlist:
newlist.append(n)
returnnewlist
# Driver Code
A=list()
n=int(input("Enter the size of the List ::"))
print("Enter the number ::")
fori in range(int(n)):
k=int(input(""))
A.append(int(k))
print("THE NEW LIST IS ::>",removeduplicateele(A))输出
Enter the size of the List ::5 Enter the number :: 10 20 30 20 10 THE LIST IS ::> [10, 20, 30]
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志