python快速提取序列中单一元素(去掉序列中重复的元素)

版权声明: https://blog.csdn.net/eacxzm/article/details/79950663

>>> import random
>>> listRandom = [random.choice(range(100)) for i in range(200)]    #在1-100中生成200个数字
>>> listRandom
[9, 2, 3, 99, 45, 41, 12, 5, 14, 88, 10, 55, 54, 93, 87, 11, 83, 94, 2, 46, 49, 59, 83, 51, 20, 68, 46, 16, 80, 47, 98, 66, 24, 83, 52, 31, 17, 53, 61, 35, 58, 9, 73, 55, 65, 38, 20, 65, 14, 55, 62, 80, 43, 29, 19, 31, 73, 69, 2, 0, 94, 30, 59, 75, 93, 53, 29, 9, 83, 31, 90, 82, 46, 73, 23, 17, 0, 55, 8, 7, 78, 0, 79, 19, 31, 75, 48, 73, 24, 55, 99, 7, 45, 3, 64, 90, 72, 15, 81, 4, 55, 38, 44, 30, 39, 61, 15, 80, 55, 45, 14, 71, 36, 28, 90, 70, 36, 10, 28, 16, 67, 67, 54, 60, 33, 52, 56, 44, 99, 52, 90, 61, 68, 4, 78, 36, 31, 62, 20, 34, 8, 34, 20, 96, 52, 39, 97, 98, 41, 20, 43, 26, 26, 96, 14, 76, 49, 29, 7, 26, 48, 86, 75, 50, 58, 61, 51, 79, 37, 40, 71, 27, 84, 25, 50, 96, 35, 73, 81, 18, 79, 60, 96, 37, 55, 71, 79, 17, 36, 35, 13, 44, 14, 31, 67, 41, 29, 89, 65, 20]
>>> noRepeat = []    
>>> for i in listRandom :    #筛选出200个数字中不重复的数字组成一个列表
 if i not in noRepeat :
  noRepeat.append(i)
  
>>> noRepeat
[9, 2, 3, 99, 45, 41, 12, 5, 14, 88, 10, 55, 54, 93, 87, 11, 83, 94, 46, 49, 59, 51, 20, 68, 16, 80, 47, 98, 66, 24, 52, 31, 17, 53, 61, 35, 58, 73, 65, 38, 62, 43, 29, 19, 69, 0, 30, 75, 90, 82, 23, 8, 7, 78, 79, 48, 64, 72, 15, 81, 4, 44, 39, 71, 36, 28, 70, 67, 60, 33, 56, 34, 96, 97, 26, 76, 86, 50, 37, 40, 27, 84, 25, 18, 13, 89]
>>> len(noRepeat)
86
>>> len(listRandom)

200
>>> newSet = set(listRandom)    #把列表放到集合中
>>> newSet
{0, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 93, 94, 96, 97, 98, 99}
>>> len(newSet)
86
阅读更多

没有更多推荐了,返回首页