Python處理重復(fù)值的方法有以下幾種:
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()
函數(shù)去除重復(fù)值。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]
以上是一些常見的方法,可以根據(jù)實(shí)際情況選擇合適的方法。