zoukankan      html  css  js  c++  java
  • Python函数

    在python中,strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象。

    Python 的值传递,还是引用传递,查看下面的例子(引用传递,即传递地址):   Python一定是引用传递的。

    from ctypes import *
    import os.path  
    import sys
    
    def test(c):
        print ("test before ")
        print (id(c))
        c+=2
        print ("test after +")
        print (id(c))
        return c
    
    def printIt(t):
        for i in range(len(t)):
            print (t[i])
    
    if __name__=="__main__":
        a=2
        print ("main before invoke test")
        print (id(a))
        n=test(a)
        print ("main after invoke test")
        print (a)
        print (id(a))

    main before invoke test
    8791192975168
    test before 
    8791192975168
    test after +     #Python是引用传递,但是numbers是不可变对象,经过计算c从2变成4之后,c开辟了一个新的空间,内存地址发生了变化
    8791192975232
    main after invoke test
    2
    8791192975168
    from ctypes import *
    import os.path  
    import sys
    
    def test(list2):
        print ("test before ")
        print (id(list2))
        list2[1]=30
        print ("test after +")
        print (id(list2))
        return list2
    
    def printIt(t):
        for i in range(len(t)):
            print (t[i])
    
    if __name__=="__main__":
        list1=["loleina",25,'female']
        print ("main before invoke test")
        print (id(list1))
        list3=test(list1)
        print ("main afterf invoke test")
        print (list1)
        print (id(list1))
    
    main before invoke test
    82843840
    test before 
    82843840
    test after +
    82843840             #list是可变对象,虽然值发生了变化,但是对应的地址没有变化
    main afterf invoke test
    ['loleina', 30, 'female']
    82843840

     

    函数参数的传递*arg,**karg

    def foo(x,*arg,**karg):
        print(x)
        print(arg)
        print(karg)
    
    foo(1,2,3,4,5,name='Alex',age=20)
    
    1
    (2, 3, 4, 5)          #*arg将所有的参数作为元祖一起传递过来
    {'name': 'Alex', 'age': 20}            #**karg将剩余的参数以字典的形式传递过来
    

    常用的函数zip,lambda,map,reduce,filter

    zip函数:

    a = [1,2,3]
    b = [4,5,6]
    c = [4,5,6,7,8]
    zipped = list(zip(a,b))
    print(zipped)
    
    print(list(zip(*zipped)))
    
    [(1, 4), (2, 5), (3, 6)]    #zip函数实现两个列表的合并,返回的是一个对象,必须使用List方法将其列表显示
    [(1, 2, 3), (4, 5, 6)]      #*zip代表解压  

      

    lambda函数:没有名字,称为“丢弃函数”,使用一次后就可以丢弃了。其表达式为lambda 变量:表达式

    la = lambda x,y: x+y
    print (la(2,3))
    
    5
    

      

    map函数: map(function, iterable, ...)

    m = map(lambda x:x*x, [1,2,3,4,5])
    print(list(m))
    
    [1, 4, 9, 16, 25]

        m = map(lambda x,y:x+y, [1,2,3,4,5],[1,2,3,4,5])
        print(list(m))

       [2, 4, 6, 8, 10]

    reduce函数:

    from functools import reduce
    reduce(lambda x, y: x+y, [1,2,3,4,5]) 
    
    15
    

    filter函数:

    fi = filter(lambda x: x%2==0, [1,2,3,4,5]) 
    print (list(fi))
    
    [2, 4]
    

      

      

     

     

  • 相关阅读:
    gnuplot learn note
    command line text process
    raspberry pi boot without HDMI
    gnuplot运行方式
    读取外部excel文件
    DB2中Lob is closed. ERRORCODE=4470的解决
    Myeclipse项目编码
    Json使用
    数组元素全排列递归算法
    XmlHttpRequest IE 乱码问题
  • 原文地址:https://www.cnblogs.com/python-study/p/14057020.html
Copyright © 2011-2022 走看看