『壹』 python列表之間去重復
使用模塊固然重要,但模塊也不可能兼顧所有的業務,你也不可能了解模塊所有的功能,和使用方法。那麼有些業務就需要我們自行完善,這個也是很多教程作業不允許代碼使用import的原因。
那麼你的這個業務需求邏輯也很簡單。若干個數的集合,隨機抽取2個,並將這兩個數從集合中刪除,如此循環
『貳』 python 如何使數組中的元素不重復
python使數組中的元素不重復的方法:
python字典的鍵都是唯一的,可以用python字典的fromkeys()方法去除列表裡面重復的元素,然後用list對象將字典的key轉換成列表,最後輸出這個列表就可以了
示例代碼如下:
執行結果如下:
更多Python知識,請關註:Python自學網!!
『叄』 python如何實現刪除某list中所有重復出現的元素
1. 使用內置函數set
lists = [1,1,2,3,4,6,6,2,2,9]
lists = list(set(lists))
先將列表轉換為集合,因為集合是不重復的,故直接刪除重復元素,而且輸出結果為排序後的
『肆』 Python如何對列表進行去重
1.使用set的特型,python的set和其他語言類似,是一個無序不重復元素集
orgList=[1,0,3,7,7,5]
#list()方法是把字元串str或元組轉成數組
formatList=list(set(orgList))
print(formatList)
結果:
[0,1,3,5,7]
2.使用keys()方法
orgList=[1,0,3,7,7,5]
#list()方法是把字元串str或元組轉成數組
formatList=list({}.fromkeys(orgList).keys())
print(formatList)
結果:
[0,1,3,5,7]
上面兩種方法的問題是:結果是沒有保持原來的順序。
3.循環遍歷法
orgList=[1,0,3,7,7,5]
formatList=[]
foridinorgList:
ifidnotinformatList:
formatList.append(id)
print(formatList)
結果:
[1,0,3,7,5]
這樣的代碼不夠簡潔
4.按照索引再次排序
orgList=[1,0,3,7,7,5]
formatList=list(set(orgList))
formatList.sort(key=orgList.index)
print(formatList)
結果:
[1,0,3,7,5]
『伍』 python刪除list重復元素
在Python中主要有5種方式 。
1、使用set函數
set是定義集合的,無序,非重復
numList = [1,1,2,3,4,5,4]
print(list(set(numList)))
#[1, 2, 3, 4, 5]
2、先把list重新排序,然後從list的最後開始掃描
a = [1, 2, 4, 2, 4, 5,]
a.sort()
last = a[-1]
for i in range(len(a) - 2, -1, -1):
if last == a[i]:
del a[i]
else:
last = a[i]
print(a) #[1, 2, 4, 5]
3、使用字典函數
a=[1,2,4,2,4,]
b={}
b=b.fromkeys(a)
c=list(b.keys())
print(c) #[1, 2, 4]
4、append方式
def delList(L):
L1 = []
for i in L:
if i not in L1:
L1.append(i)
return L1
print(delList([1, 2, 2, 3, 3, 4, 5])) #[1, 2, 3, 4, 5]
5、count + remove方式
def delList(L):
for i in L:
if L.count(i) != 1:
for x in range((L.count(i) - 1)):
L.remove(i)
return L
print(delList([1, 2, 2, 3, 3, 4]))#[1, 2, 3, 4]
『陸』 python如何刪除list里重復的元素
這個可簡單可復雜。
簡單的:利用集合
a=list(set(a))#這樣就可以了,是不是很簡單
復雜的:
就是先對列表排序,然後比較相鄰元素是否相同,相同的則刪除後面的。大體演算法思路,代碼自己寫吧
當然還有其他的演算法...
-----------------------------------------------------------------------------------
額, 不好意思,看錯了
defQ(a):
aa=[]
foriina:
ifa.count(i)==1:
aa.append(i)
returnaa
這個就可以了,利用 list.count(obj) 計算obj在list中出現的次數進行判斷
『柒』 python中如何刪除列表中重復的元素
舉個例子,比如有這樣一個列表l,可表示為[1,2,2,5,3,6],它可以通過下面的操作達到去重的目的:l=list(set(l)),在python shell中代碼執行如下圖所示:
python列表去重操作代碼運行結果
『捌』 python如何刪除list里重復的元素
一共使用四種方法來去除列表中的重復元素,下面是具體實現:
def f1(seq):
# not order preserving
set = {}
map(set.__setitem__, seq, [])
return set.keys()
def f2(seq):
# order preserving
checked = []
for e in seq:
if e not in checked:
checked.append(e)
return checked
def f3(seq):
# Not order preserving
keys = {}
for e in seq:
keys[e] = 1
return keys.keys()
def f4(seq):
# order preserving
noDupes = []
[noDupes.append(i) for i in seq if not noDupes.count(i)]
return noDupes
def f5(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
def f6(seq):
# Not order preserving
set = Set(seq)
return list(set)
『玖』 python123上一道習題刪除列表中重復元素求代碼,或者幫我看看我的代碼怎麼改,初學,我感覺好像要大改
代碼如下,:
import random
m = int(input("m:"))
n = int(input("n:"))
random.seed(m)
randoms = [str(random.randint(0, 9)) for i in range(10)]
sort_randoms = list(set(randoms))
sort_randoms = sorted(sort_randoms, key=int)
print(randoms)
print(sort_randoms)
輸出:
['9', '0', '6', '7', '9', '0', '3', '7', '7', '4']
['0', '3', '4', '6', '7', '9']