Python 資料列表簡單去重

2021-09-29 03:17:16 字數 819 閱讀 6533

def remove_same(list):

list.sort() # 不需要重新排序的話刪除此句即可

new_list =

for i in list:

if i not in new_list:

return new_list

lists = [1,3,2,4,9,2,3,5,6,7,10,66,23,33,4,2]

remove_same(lists)

def remove_same(list):

list.sort() # 不需要重排序刪除此句即可

# new_list =

for i in list:

if lists.count(i) >= 2:

list.pop(list.index(i)) # pop()引數是元素的索引

return list

lists = [1,3,2,4,9,2,3,5,6,7,10,66,23,33,4,2]

remove_same(lists)

注:set()函式建立乙個無序不重複元素集,可進行關係測試,刪除重複資料,還可以計算交集、差集、並集等。

def remove_same(list):

new_list = set(list)

return new_list

lists = [1,3,2,4,9,2,3,5,6,7,10,10,66,23,33,4,2,3]

remove_same(lists)

python 多表去重 Python列表去重

無聊統計了下列表去重到底有多少種方法。1.集合 list set alist 如果要保持順序 import random if name main a random.randint 0,10 for i in xrange 10 b list set a b.sort key a.index 2.字...

Python列表去重

標題有語病,其實是這樣的 假設有兩個列表 l1 1,2,3,4 l2 1,2,5,6 然後去掉l1中包含的l2的元素 直接這樣當然是不行的 def removeexists l1,l2 for e1 in l1 if e1 in l2 l1.remove e1 不管什麼語言都不能這麼幹,但是又有一點...

hive 列表去重 Hive 資料去重

實現資料去重有兩種方式 distinct 和 group by 1.distinct消除重複行 distinct支援單列 多列的去重方式。單列去重的方式簡明易懂,即相同值只保留1個。多列的去重則是根據指定的去重的列資訊來進行,即只有所有指定的列資訊都相同,才會被認為是重複的資訊。1 作用於單列 se...