当前位置: 首页 > 后端技术 > Python

Pythonstringandlisttodeduplicate

时间:2023-03-26 11:09:29 Python

stringdeduplication1.使用集合——不保留原来的顺序print(set(pstr))2.使用字典——不保留原来的顺序print({}.fromkeys(pstr.keys())3.使用循环遍历的方式——代码不够简洁,不够高端a=[]foriinrange(len(pstr)):ifpstr[i]notina:a.append(pstr[i])print(a)listdeduplicationplist=[1,0,3,7,5,7]1、使用set方法print(list(set(plist)))2、使用字典print(list({}.fromkeys(plist).keys()))3。循环遍历方法plist1=[]foriinplist:ifinotinplist1:plist1.append(i)print(plist1)4.根据索引重新排序b=list(set(plist))b.sort(key=plist.index)print('sds:',b)