Python string list deduplication

Weight to a relatively high frequency strings, and lists recent interview 
pstr = 'abcadcf'
# deduplication string
# 1, using the set - is not maintained in the original sequence
print(set(pstr))
# 2, using a dictionary - did not keep the original order
print({}.fromkeys(pstr).keys())
# 3, using a loop through method - the code is simple enough, not high-end
a = []
for i in range(len(pstr)):
    if pstr[i] not in a:
        a.append(pstr[i])
print(a)
# List deduplication
plist = [1,0,3,7,5,7]
# 1, using the set method
print(list(set(plist)))
# 2, use the dictionary
print(list({}.fromkeys(plist).keys()))
# 3, loop through Law
plist1 = []
for i in plist:
    if i not in plist1:
        plist1.append(i)
print(plist1)
# 4, according to the index ordering again
b = list(set(plist))
b.sort(key=plist.index)
print('sds:',b)

Guess you like

Origin www.cnblogs.com/xyf9575/p/11094387.html