问题
I have a list lst = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,4]
I'm expecting the following output:
out = [1,"","",2,"","","",3,"","","","",4,"","","","","","","",""]
I want to keep the first occurrence of the item and replace all other occurrences of the same item with empty strings.
I tried the following approach.
`def splrep(lst):
from collections import Counter
C = Counter(lst)
flst = [ [k,]*v for k,v in C.items()]
nl = []
for i in flst:
nl1 = []
for j,k in enumerate(i):
nl1.append(j)
nl.append(nl1)
ng = list(zip(flst, nl))
for i,j in ng:
j.pop(0)
for i,j in ng:
for k in j:
i[k] = ''
final = [i for [i,j] in ng]
fin = [i for j in final for i in j]
return fin`
But I'm looking for some simpler or better approaches.
回答1:
Use itertools.groupby, quite appropriate for grouping consecutively duplicate values.
from itertools import groupby
[v for k, g in groupby(lst) for v in [k] + [""] * (len(list(g))-1)]
# [1, '', '', 2, '', '', '', 3, '', '', '', '', 4, '', '', '', '', '', '', '', '']
If your list values are not consecutive, you may sort them first.
回答2:
Using a simple iteration
Ex:
lst = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,4]
dup_check = set()
result = []
for i in lst:
if i not in dup_check:
result.append(i)
dup_check.add(i)
else:
result.append("")
print(result)
Output:
[1, '', '', 2, '', '', '', 3, '', '', '', '', 4, '', '', '', '', '', '', '', '']
回答3:
Try this simpler function:
def fill_blank(l):
filled = []
last = l[0]
for i in l:
if i != last:
last = i
filled.append(last)
return filled
Use it as such:
>>> lst = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4]
>>> out = fill_blank(lst)
>>> print(out)
[1, '', '', 2, '', '', '', 3, '', '', '', '', 4, '', '', '', '', '', '', '', '']
回答4:
Short solution (one-liner) without any modules usage is as follows:
lst = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,4]
out = ["" if (i in lst[:inx]) else i for inx,i in enumerate(lst)]
print(out)
output
[1, '', '', 2, '', '', '', 3, '', '', '', '', 4, '', '', '', '', '', '', '', '']
回答5:
Do this if you don't want to use set
or itertools
:
lst = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,4]
new_lst = []
for i in lst:
if i in new_lst:
new_lst.append("")
else:
new_lst.append(i)
print(new_lst)
Output:
[1, '', '', 2, '', '', '', 3, '', '', '', '', 4, '', '', '', '', '', '', '', '']
回答6:
You can try below approach..
lst = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,4]
fin=[]
for each in lst:
if each not in fin:
fin.append(each)
else:
fin.append("")
print(fin)
回答7:
You can store the first occurences in a dictionary with dict.setdefault(), then keep the first found numbers depending if they exist in this dictionary:
lst = [1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,4]
first_indices = {}
for i, x in enumerate(lst):
first_indices.setdefault(x, i)
result = [x if first_indices[x] == i else "" for i, x in enumerate(lst)]
print(result)
# [1, '', '', 2, '', '', '', 3, '', '', '', '', 4, '', '', '', '', '', '', '', '']
来源:https://stackoverflow.com/questions/54037427/replace-duplicate-items-from-list-while-keeping-the-first-occurrence