zoukankan      html  css  js  c++  java
  • python: 列表的方法

    操作

    函数

    使用方法

    备注

    索引

    index

    in: example.index(‘creative’)

    --- 1

    in:example[1,]

    --- [’creative’, [’京东’,996]]

     

    赋值

     

    in: example[1] = [‘commonplace’]

    --- [1, ‘commonplace’, [’京东’,996]]

    in: example[2,] = [[‘国企’,855],[‘阿里’,996],[‘博士生’,007]]

     

    删除

    clear

    pop

    索引赋空值

    in: example.clear

    --- []

    in: example.pop()

    --- [’京东’,996]( example== [1, ’creative’])

    in: example[1] = []

    ---  [1,  [’京东’,996]]

    clear:就地清空列表的内容

    pop : 从列表中删除一个元素,并返回这一元素

    插入

    insert

    索引赋值

    in: example.insert(1,’ commonplace’)

    --- [1, ’ commonplace’,’creative’, [’京东’,996]]

    in: example[1:1]=[ ’ anyway’]

    --- [1, ’ anyway’ ,’creative’, [’京东’,996]]

     

    扩展与拼接

    extend

    append

    a = [1,2,3];b= [4,5,6]

    in: a.extend(b)

    ---- [1,2,3,4,5,6]

    in: a.append(7)

    --- [1,2,3,7]

    in: a+b

    [1,2,3,4,5,6](但是,a未改变)

     

    复制

    copy

    ex = example

    ex[3] = [‘奋斗==全勤投入?’]

    in: example

    ---  [1, ’creative’, [’京东’,996],‘奋斗==全勤投入?]

    in: ex= example.copy()

    ex[3] = [‘奋斗==全勤投入?’]

    in: example

    --- [1, ’creative’, [’京东’,996]]

     “=”,传递位置(指针)

    copy:只复制元素

     

    计数

    count

    in: [‘stay’,’hungrey’,’stay’,’foolish’].count(‘stay’)

    --- 2

     

    排序

    sort

    sorted

    x=[4,6,2,1,7,9]

    x.sort()

    in: x

    --- [1,2,4,7,9]

    in: y=sorted(x),    x

    --- [1,2,4,7,9],    [4,6,2,1,7,9]

    sort用于对列表就地排序;

    sorted不改变原来类别的元素的顺序

     

  • 相关阅读:
    兄弟连学python(1)——MySQL
    运算和运算符相关知识
    关于python中的快捷键
    关于爬虫
    Hello Python
    [ARC101C] Ribbons on Tree
    CF568E Longest Increasing Subsequence
    2021省选游记
    [NEERC2015]Distance on Triangulation
    dp的一些优化
  • 原文地址:https://www.cnblogs.com/liuxiang2020/p/10758989.html
Copyright © 2011-2022 走看看