當前位置:首頁 » 編程語言 » pythonlistof刪除

pythonlistof刪除

發布時間: 2022-05-04 04:55:24

python如何實現刪除某list中所有重復出現的元素

1. 使用內置函數set
lists = [1,1,2,3,4,6,6,2,2,9]
lists = list(set(lists))
先將列表轉換為集合,因為集合是不重復的,故直接刪除重復元素,而且輸出結果為排序後的

Ⅱ 急求:如何用python刪除文本中的重復行

1.如果你的txt文件不大的話可以直接
tmp=open('**.txt').readlines()#把內容一次性全部讀取出來是一個列表
set(tmp)#這個就是把列表去重復
然後你可以把這個去重後的tmp寫入到新的文件
2.txt很大,那麼只能一行一行的讀取去重了
#!/usr/bin/envpython
#coding=utf-8
#python2.7
outfile=open('result-readline.txt','w')#新的文件
list_1=[]
forlineinopen('test.txt'):#老文件
tmp=line.strip()
iftmpnotinlist_1:
list_1.append(tmp)
outfile.write(line)
outfile.close()

Ⅲ python list 怎麼刪除

>>>l=[1,2,3,4,5]
>>>del(l[1])
>>>l
[1,3,4,5]

del刪除某個list元素

>>>l.clear()
>>>l
[]

直接用clear清空

Ⅳ python list怎麼刪除元素

有兩個方法

1.pop()
默認刪除最後一個元素。
也可以給定一個索引值刪除索引值對應的元素。

Ⅳ python刪除list列表多個指定位置中的元素

li1=[12,3,4,5,2,34,5,6,7,3,5,6,66]
removelist=[1,2,4,5]
x=0
foryinremovelist:
li1.pop(y-x)
x+=1
printli1

這樣有一個要求就是removelist裡面的數字必須是從小到大的順序排列的,

Ⅵ python list怎麼刪除一個元素

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中怎麼刪除重復數據保留一條

可以先統計list中每個數據的個數,用一個dict存儲,然後遍歷list,判斷是否是最後一個,是的就從list中刪除即可

Ⅷ python 移除list里的元素

def remove_section(alist,start,end):
if start > len(alist):
# 開始位置越界返回原串
return alist[:]
elif end > len(alist):
# 結束位置越界
return alist[:start]
else:
a = alist[:start]
a.extend(alist[end:])
return a

Ⅸ 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)

Ⅹ python如何刪除list里重復的元素

這個可簡單可復雜。

簡單的:利用集合

a=list(set(a))#這樣就可以了,是不是很簡單

復雜的:

就是先對列表排序,然後比較相鄰元素是否相同,相同的則刪除後面的。大體演算法思路,代碼自己寫吧

當然還有其他的演算法...

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

額, 不好意思,看錯了

defQ(a):
aa=[]
foriina:
ifa.count(i)==1:
aa.append(i)
returnaa

這個就可以了,利用 list.count(obj) 計算obj在list中出現的次數進行判斷

熱點內容
搭建httpsgit伺服器搭建 發布:2025-05-14 13:09:47 瀏覽:253
新電腦拿回來我該怎麼配置 發布:2025-05-14 13:09:45 瀏覽:238
視頻伺服器新建ftp用戶 發布:2025-05-14 13:03:09 瀏覽:225
php花生 發布:2025-05-14 12:54:30 瀏覽:550
java人才 發布:2025-05-14 12:29:10 瀏覽:649
如何打開軟密碼 發布:2025-05-14 12:28:55 瀏覽:427
七牛存儲待遇 發布:2025-05-14 12:27:20 瀏覽:422
C語言a35a4a5 發布:2025-05-14 11:53:48 瀏覽:813
android隱藏item 發布:2025-05-14 11:43:56 瀏覽:328
javawebeclipse編譯 發布:2025-05-14 11:35:24 瀏覽:938