欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)去除列表中重復(fù)元素的方法小結(jié)【4種方法】

 更新時間:2018年04月27日 10:14:58   作者:Together_CZ  
這篇文章主要介紹了Python實現(xiàn)去除列表中重復(fù)元素的方法,結(jié)合實例形式總結(jié)分析了Python列表去重的4種實現(xiàn)方法,涉及Python針對列表的遍歷、判斷、排序等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)去除列表中重復(fù)元素的方法。分享給大家供大家參考,具體如下:

這里一共使用了四種方法來去除列表中的重復(fù)元素,下面是具體實現(xiàn):

#!usr/bin/env python
#encoding:utf-8
'''
__Author__:沂水寒城
功能:去除列表中的重復(fù)元素
'''
def func1(one_list):
  '''''
  使用集合,個人最常用
  '''
  return list(set(one_list))
def func2(one_list):
  '''''
  使用字典的方式
  '''
  return {}.fromkeys(one_list).keys()
def func3(one_list):
  '''''
  使用列表推導(dǎo)的方式
  '''
  temp_list=[]
  for one in one_list:
    if one not in temp_list:
      temp_list.append(one)
  return temp_list
def func4(one_list):
  '''''
  使用排序的方法
  '''
  result_list=[]
  temp_list=sorted(one_list)
  i=0
  while i<len(temp_list):
    if temp_list[i] not in result_list:
      result_list.append(temp_list[i])
    else:
      i+=1
  return result_list
if __name__ == '__main__':
  one_list=[56,7,4,23,56,9,0,56,12,3,56,34,45,5,6,56]
  print "腳本之家測試結(jié)果:"
  print func1(one_list)
  print func2(one_list)
  print func3(one_list)
  print func4(one_list)

結(jié)果如下:

腳本之家測試結(jié)果:
[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]
[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]
[56, 7, 4, 23, 9, 0, 12, 3, 34, 45, 5, 6]
[0, 3, 4, 5, 6, 7, 9, 12, 23, 34, 45, 56]

運行結(jié)果截圖:

PS:本站還有兩款比較簡單實用的在線文本去重復(fù)工具,推薦給大家使用:

在線去除重復(fù)項工具:
http://tools.jb51.net/code/quchong

在線文本去重復(fù)工具:
http://tools.jb51.net/aideddesign/txt_quchong

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python字典操作技巧匯總》、《Python字符串操作技巧匯總》、《Python常用遍歷技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論