zoukankan      html  css  js  c++  java
  • Python 小结

    1.

    Python pass是空语句,是为了保持程序结构的完整性。

    pass 不做任何事情,一般用做占位语句。

    2.删除一个list里面的重复元素

    方法一:是利用map的fromkeys来自动过滤重复值,map是基于hash的,大数组的时候应该会比排序快点吧

    # *-* coding:utf-8 *-*
    def distFunc1():
        a=[1,2,4,2,4,5,6,5,7,8,9,0]
        b={}
        b=b.fromkeys(a)
        print b
        #print b.keys()
        a=list(b.keys())
        print a
    
    distFunc1()
    

      

    方法二:是用set(),set是定义集合的,无序,非重复

    >>> a = [1, 3, 2, 2, 1, 5, 5, 3]
    >>> a = list( set(a) )
    >>> print a
    [1, 2, 3, 5]

    方法三:是排序后,倒着扫描,遇到已有的元素删之

     1 # *-* coding:utf-8 *-*
      2 def distFunc1():
      3     list1 = [1,2,4,2,4,5,6,5,7,8,9,0]
      4     if list1:
      5         list1.sort()
      6         last = list1[-1]
      7         for i in range(len(list1)-2, -1, -1):
      8             if last == list1[i]:
      9                 del list1[i]
     10             else: 
     11                 last = list1[i]
     12         print list1
     13 distFunc1()



    ~             

  • 相关阅读:
    面向对象 小游戏 打飞机
    面向对象2
    面向对象
    正则 校验邮箱
    正则 过滤敏感字
    Strobogrammatic Number
    Binary Tree Right Side View
    [?]*Closest Binary Search Tree Value II
    *Closest Binary Search Tree Value
    *Inorder Successor in BST
  • 原文地址:https://www.cnblogs.com/code-charmer/p/6375728.html
Copyright © 2011-2022 走看看