1. Remove duplicate elements from the list
#_*_coding:utf-8_*_ls= [1,3,6,9,2,5,1,3,7,8,6]Print('ls >>>', ls)#Method 1LS1 =list (set (LS))Print('LS1 >>>', LS1)#Method 2LS2 ={}.fromkeys (ls). Keys () LS2= [L forLinchLS2]Print('LS2 >>>', LS2)#Method 3LS3 =list (set (LS)) Ls3.sort (Key=ls.index)#maintain the order of the original list elementsPrint('LS3 >>>', LS3)#Method 4LS4 =[][ls4.append (i) forIinchLsifI not inchLS4]Print('LS4 >>>', LS4)
2. Delete the specified file
# _*_coding:utf-8_*_ Import 't.txt'os.remove (filepath)
3. Read the contents of the file and print it on the screen
With open ('1.txt','r') as fp: = Fp.readlines () print(contents) for in Contents: Print (Content.replace ('\ n',') ")
5. The quickest way to get the list element backwards
ls = [1,3,6,9,2,5,1,3,7,8,6= ls[::-1]
4. Conversion of list and tuple to each other
TS = (1, 3, 6, 9, 2, 5, 1, 3, 7, 8, 6= [1, 3, 6, 9, 2, 5, 1, 3, 7, 8, 6]list (ts) tuple (LS)
Python face question