当前位置:Gxlcms > Python > python读取TXT到数组及列表去重后按原来顺序排序的方法

python读取TXT到数组及列表去重后按原来顺序排序的方法

时间:2021-07-01 10:21:17 帮助过:67人阅读

本文实例讲述了python读取TXT到数组及列表去重后按原来顺序排序的方法。分享给大家供大家参考。具体如下:

  1. ####################################################################
  2. # python 读取TXT到数组 列表去重,不打乱原来的顺序
  3. # 刚学写的不好请大家见谅
  4. ####################################################################
  5. def open_txt(): #打开TXT文本写入数组
  6. try:
  7. infile = file('admin.txt', 'r')
  8. xxx = file('admin.txt', 'r')
  9. for xxx_line in xxx.readlines():
  10. passlist.append(xxx_line)
  11. xxx.close()
  12. except:
  13. return 0
  14. def list_del(): #清空list列表
  15. try:
  16. i = 0 #得到list的第一个元素
  17. while i < len(passlist):
  18. del passlist[i]
  19. del list_passwed[i]
  20. except:
  21. return 0
  22. ######################################
  23. global passlist #声明全局变量
  24. passlist = [] #用户名:anonymous 密码为空
  25. www_cj(www) #域名拆解
  26. open_txt() #TXT导入数组
  27. #passlist = list(set(passlist)) #python 列表去重
  28. global list_passwed #列表去重,不打乱原来的顺序
  29. list_passwed=[]
  30. for i in passlist:
  31. if i not in list_passwed:
  32. list_passwed.append(i)
  33. ###################################### 遍历数组组合出 密码
  34. I1 = 0 #得到list的第一个元素
  35. while I1 < len(list_passwed):
  36. print "WWWWWWWWWWW",I1
  37. if I1==len(list_passwed):
  38. break #退出循环
  39. I2 = 0 #得到list的第一个元素
  40. while I2 < len(list_passwed):
  41. print "1111:",list_passwed[I1],"2222:",list_passwed[I2]
  42. I2 = I2 + 1 #二层
  43. I1 = I1 + 1 #一层

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行