Remove duplicate list in Python

Method one

def distinct(list_1):
    list_2 = set(list_1)
    list_3 = list(list_2)
    print(list_3)

def main():
    list_1 = [1,1,6,3,1,5,2]
    distinct(list_1)

if __name__ == '__main__':
   main()


---------------results of enforcement----------
[1, 2, 3, 5, 6]

-----------------------------------------------

Method two

def distinct(list_1):
    for num in list_1:
        if num not in list_2 :
            list_2.append(num)
    print(list_2)

def main():
    list_1 = [1,1,6,3,1,5,2]
    list_2 = []
    distinct(list_1)

if __name__ == '__main__':
   main()


---------------results of enforcement----------
[1, 6, 3, 5, 2]

-----------------------------------------------

Summary:

Method one: List ascending sorting output

Method two: out of order output

猜你喜欢

转载自blog.csdn.net/Xcq007/article/details/81937461
今日推荐