• python函数


    filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

      该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中

      注意: Pyhton2.7 返回列表,Python3.x 返回迭代器对象(对象的内存地址)

    map(function, sequence[, sequence, ...]) -> list 一组序列,即可迭代对象

      for example map(lambada x:x+1,[1,2,3,4,]) 可以添加多个序列,多个序列作为参数传递个lambda函数

    enumerate() 内置函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中

    >>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
    >>> list(enumerate(seasons))
    输出 [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
    
    
    >>>seq = ['one', 'two', 'three']
    >>> for i, element in enumerate(seq):
    ...     print i, element
    ... 
    0 one
    1 two
    2 three

    format(*args,**kwargs)   

      Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。

    基本语法是通过 {} 和 : 来代替以前的 %(详见http://www.runoob.com/python/att-string-format.html)

    print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
     
    # 通过字典设置参数
    site = {"name": "菜鸟教程", "url": "www.runoob.com"}
    print("网站名:{name}, 地址 {url}".format(**site))
     
    # 通过列表索引设置参数
    my_list = ['菜鸟教程', 'www.runoob.com']
    print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # "0" 是必须的
    View Code
    >>>"{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
    'hello world'
     
    >>> "{0} {1}".format("hello", "world")  # 设置指定位置
    'hello world'
     
    >>> "{1} {0} {1}".format("hello", "world")  # 设置指定位置
    'world hello world'
    View Code
    也可以传入实例化对象
    class AssignValue(object):
        def __init__(self, value):
            self.value = value
    my_value = AssignValue(6)
    print('value 为: {0.value}'.format(my_value))  # "0" 是可选的
    View Code

     对一个数据进行去重并按照原来的排序

    list1 = [11,23,45,34,11,45]
    #先去重
    ret = set(list1)
    #排序
    ret1 = soted(ret,key=list1.index) //key是排序规则,默认是值得大小

    补充:sys.stdout.flush() #刷新缓存(数据过小时(缓存为写满),subprocess等管道拿不到缓存的内容)

    Take a small step every day
  • 相关阅读:
    Tomcat9报错 The valid characters are defined in RFC 7230 and RFC 3986
    Job for ssh.service failed because the control process exited with error code.......
    解决 ora-28001 密码过期的处理办法
    Vmware unknow Interface ens33
    ubuntu桌面环境安装中文环境
    (CRON) info (No MTA installed, discarding output)” error in the syslog
    SSH Secure Shell链接Ubuntu报错Server responded "Algorithm negotiation failed"
    mysql备份数据库出错mysqldump: [ERROR] unknown option '--no-beep'
    dclcommon200.bpl
    字节 汉字判断
  • 原文地址:https://www.cnblogs.com/qlshao/p/9521698.html
Copyright © 2020-2023  润新知