Python處理重復值的方法有以下幾種:
lst = [1, 2, 3, 3, 4, 4, 5]
lst = list(set(lst))
print(lst) # 輸出 [1, 2, 3, 4, 5]
lst = [1, 2, 3, 3, 4, 4, 5]
lst = [x for i, x in enumerate(lst) if x not in lst[:i]]
print(lst) # 輸出 [1, 2, 3, 4, 5]
from collections import Counter
lst = [1, 2, 3, 3, 4, 4, 5]
counter = Counter(lst)
lst = [x for x in counter if counter[x] == 1]
print(lst) # 輸出 [1, 2, 5]
drop_duplicates()
函數去除重復值。import pandas as pd
lst = [1, 2, 3, 3, 4, 4, 5]
df = pd.DataFrame(lst, columns=['value'])
df = df.drop_duplicates()
lst = df['value'].tolist()
print(lst) # 輸出 [1, 2, 3, 4, 5]
以上是一些常見的方法,可以根據實際情況選擇合適的方法。