• python数据类型之list


    1、append:增加元素到列表尾部

    L.append(object) -> None -- append object to end
    

    2、clear:清空列表中所有元素

    3、count:返回列表中指定值的数量

    L.count(value) -> integer -- return number of occurrences of value
    

    4、extend:用列表扩展列表的元素

    #L.extend(iterable) -> None -- extend list by appending elements from the iterable
    l1 = [1,2,3]
    l2 = [3,4,5]
    l1.extend(l2)
    print(l1)
    

    5、index:返回指定元素的索引位置

    #L.index(value, [start, [stop]]) -> integer -- return first index of value
    lx = ['today','is','a','good','day']
    print(lx.index('is'))
    结果:1

    6、insert:在指定索引位置插入

    #L.insert(index, object) -- insert object before index
    lx = ['today','is','a','good','day']
    lx.insert(2,'not')
    print(lx)
    结果:['today', 'is', 'not', 'a', 'good', 'day']

    7、pop:删除列表指定位置的元素,默认为最后一个元素

    #L.pop([index]) -> item -- remove and return item at index (default last)
    lx = ['today','is','a','good','day']
    lx.pop(2)
    print(lx)
    结果:['today', 'is', 'good', 'day']

    8、remove:删除列表中指定的元素

    #L.remove(value) -> None -- remove first occurrence of value
    lx = ['today','is','a','good','day']
    lx.remove('a')
    print(lx)
    结果:['today', 'is', 'good', 'day']

    9、reverse:将列表中所有元素反转

    #L.reverse() -- reverse *IN PLACE*
    lx = ['today','is','a','good','day']
    lx.reverse()
    print(lx)
    结果:['day', 'good', 'a', 'is', 'today']

    10、sort:排序

    #原址排序
    x= [3,2,4,6,5,7,9,8]
    x.sort()
    print(x)
    结果:[2, 3, 4, 5, 6, 7, 8, 9]
    
    #需要一个排序好的副本,通过切片方法将x赋值给y
    x= [3,2,4,6,5,7,9,8]
    y = x[:]
    y.sort()
    print(x)
    print(y)
    结果:
    [3, 2, 4, 6, 5, 7, 9, 8]
    [2, 3, 4, 5, 6, 7, 8, 9]
    
    #再一种通过sorted
    x= [3,2,4,6,5,7,9,8]
    y = sorted(x)
    print(x)
    print(y)
    结果:
    [3, 2, 4, 6, 5, 7, 9, 8]
    [2, 3, 4, 5, 6, 7, 8, 9]

     11、filter:使用一个自定义的涵数过滤一个序列,把函数的每一项传到自定义的函数里处理,最终一次性返回过滤的结果

    def func(x):
        if x >33:
            return True
    
    li = [11,22,33,44,55]
    new_list = filter(func,li)
    print(list(new_list))
    结果:[44,55]

    12、zip:接受任意多个序列,返回一个元组

    x = [1,2,3]
    y = [4,5,6]
    z = [7,8,9]
    zipped = zip(x,y,z)
    yy = list(zipped)
    xyz =zip(*yy)   #zip的反函数
    print(yy)
    print(list(xyz))
    结果:
    [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

    13、tell:查看当前指针位置

      seek:定位当前指针位置

    '''test.llog
    孔kongzhagen
    '''
    
    f = open('test.log','r+',encoding='utf-8')
    f.read(2)
    print(f.tell()) #查看当前指针位置
    
    f.seek(6)
    f.read(2)
    print(f.tell())
    结果:4,8

    14、truncate: truncate(n):  从文件的首行首字符开始截断,截断文件为n个字符;无n表示从当前位置起截断;截断之后n后面的所有字符被删除。其中win下的换行代表2个字符大小。

    '''
    # Assuming file has following 5 lines
    # This is 1st line
    # This is 2nd line
    # This is 3rd line
    # This is 4th line
    # This is 5th line
    '''
    f = open("test.log","r+")
    print('Name of file %s'%f.name)
    line = f.readline()
    print('Read line:%s'%line)
    f.truncate(f.tell()) #文件保留了第一行,其余被截断
    line = f.readline()
    print('Read line:%s'%line)
    f.close()
    结果:
    Name of file test.log
    Read line:# Assuming file has following 5 lines
    
    Read line:# This is 1st line

     其它

    numList = [1,2,2,23,4,5]
    strList = ['a','b','c','d','e']
    numList.append(6)  # numList列表增加一个值
    print numList
    print numList.count(2)  # 列表中2出现的次数
    numList.extend(strList)  # strList的内容增加到numList后面
    print numList
    print numList.index(23)  # numList中23的索引位置
    numList.insert(3,45)   # 在numList的3索引位置添加45
    print numList
    print numList.pop()  # 获取并删除numList最后一个值
    numList.remove(4)  # 从numList中删除4
    print numList
    numList.reverse()  # 获取numList的反向列表
    print numList
  • 相关阅读:
    mysql官网下载yum
    zookeeper和kafka的leader和follower
    查看目标端口是否被占用
    scala中的val,var和lazy
    scala的异常处理try catch
    Navicat总是提示主键不存在问题
    idea常用快捷键
    wiremock技术入门
    Liunx常用操作(11)-VI编辑器-末行模式命令
    Liunx常用操作(十)-VI编辑器-命令模式命令
  • 原文地址:https://www.cnblogs.com/kongzhagen/p/5449212.html
Copyright © 2020-2023  润新知