zoukankan      html  css  js  c++  java
  • python 函数2

    nums = [1,2,3,4,5]

    map函数

      map(函数,序列)

    map 是把数组中的值一个一个的进行某种处理,把处理后的值放到一个新的数组中,并返回这个新的数组。

    map(lambda x:x++2,[1,2,3,4])

    返回的是可迭代对象

    for i in map(lambda x:x++2,[1,2,3,4]):

      print(i)

    3
    4
    5
    6

    def test(x):
      if(x>=1):
        x = x - 1
        return x+test(x)
      else:
        return 0

    for i in map(test,[1,2,3,4]):
    print(i)

    0
    1
    3
    6

    >>> list(map(test,[1,2,3,4]))
    [0, 1, 3, 6]

    filter函数

    filter是通过某种筛选条件把数组中符合条件的值放到一个新的数组中,并返回这个新数组

    filter(函数,序列)

    拿出布尔值

    >>> list(filter(lambda x:x.startswith("a"),["aa","bb","abc","abd"]))
    ['aa', 'abc', 'abd']

    def test1(x):
    if(len(x) > 2):
    return True
    else:
    return False

    >>> list(filter(test1,["aa","bb","abc","abd"]))
    ['abc', 'abd']

    reduce函数

    reduce处理一个序列,把序列合并操作

    >>> def add(x, y):

         return x+y

    reduce(add, [1,2,3,4])

    10

    abs() 绝对值

    all() 拿出值做布尔预算

    bool 转化为bool值

    bytes("aa',encoding="utf-8").decode("utf-8")

    >>> bytes("选项",encoding="utf-8")
    b'xe9x80x89xe9xa1xb9'

    bytes(b'xe9x80x89xe9xa1xb9').decode("utf-8")
    '选项'

    chr() ascii码

    dir() 查看

    divmod(10,3)

    (3,1)  取商取余 

    eval() 函数

    简单表达式

    print(eval('1+2'))

    3

    字符串转字典

    print(eval("{'name':'linux','age':18}")

    {'name':'linux','age':18}

    全局变量

    globals()

    局部变量

    locals()

    max() 最大值

    min() 最小值

  • 相关阅读:
    OTA JAR和JAD的mime不同
    document.getElementById('selCatalog').remove(i)突然无效???!
    判断WAP1.1和WAP2.0并解析为wml或xhtml
    IE和firefox下显示html内容
    unixrisk tip
    unixftp windows
    unixstdin/stdout/stderr
    峰鸟摄影
    linuxgrep commond
    unixtutorial(recommended)
  • 原文地址:https://www.cnblogs.com/hywhyme/p/11580915.html
Copyright © 2011-2022 走看看