String deduplication
1. Use collections — not in the same order
print(set(pstr))
Copy the code
2. Use a dictionary — not in the same order
print({}.fromkeys(pstr).keys())
Copy the code
3. Use loop traversal — the code is not clean enough or high end
a = []
for i in range(len(pstr)):
if pstr[i] not in a:
a.append(pstr[i])
print(a)
Copy the code
List to heavy
plist = [1.0.3.7.5.7]
Copy the code
1. Use the set method
print(list(set(plist)))
Copy the code
2. Use a dictionary
print(list({}.fromkeys(plist).keys()))
Copy the code
3. Cycle traversal
#Python Learning Exchange group: 531509025
plist1 = []
for i in plist:
if i not in plist1:
plist1.append(i)
print(plist1)
Copy the code
4. Sort by index again
b = list(set(plist))
b.sort(key=plist.index)
print('sds:',b)
Copy the code