Python中的列表方法

本文是以下文章的延伸: Python列表 列出Python | Set 1中的方法(in、not in、len()、min()、max()…) 列出Python | Set 2中的方法(del、remove()、sort()、insert()、pop()、extend()…)

null

添加和附加

  • append(): 用于向列表中追加和添加元素。它用于将元素添加到列表的最后一个位置。 语法:
     list.append (element)

    # Adds List Element as value of List.
    List = [ 'Mathematics' , 'chemistry' , 1997 , 2000 ]
    List .append( 20544 )
    print ( List )

    
    

    输出:

    ['Mathematics', 'chemistry', 1997, 2000, 20544]
    

  • 插入(): 在指定位置插入元素。 语法:
     list.insert(<position, element)

    注意:提到的位置应该在列表范围内,因为在本例中介于0和4之间,否则将抛出Indexer。

    List = [ 'Mathematics' , 'chemistry' , 1997 , 2000 ]
    # Insert at index 2 value 10087
    List .insert( 2 , 10087 )
    print ( List )

    
    

    输出:

    ['Mathematics', 'chemistry', 10087, 1997, 2000, 20544]
    
  • 扩展(): 将列表2的内容添加到列表1的末尾。 语法:
    List1.extend(List2)

    List1 = [ 1 , 2 , 3 ]
    List2 = [ 2 , 3 , 4 , 5 ]
    # Add List2 to List1
    List1.extend(List2)
    print (List1)
    # Add List1 to List2 now
    List2.extend(List1)
    print (List2)

    
    

    输出:

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

List的sum()、count()、index()、min()和max()函数

  • sum(): 计算列表中所有元素的总和。 语法:
     sum(List)

    List = [ 1 , 2 , 3 , 4 , 5 ]
    print ( sum ( List ))

    
    

    输出:

    15
    

    如果数值不是参数,会发生什么? Sum只计算数值,否则会抛出TypeError。 参见示例:

    List = [ 'gfg' , 'abc' , 3 ]
    print ( sum ( List ))

    
    

    输出:

    Traceback (most recent call last):
      File "", line 1, in 
        sum(List)
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    
  • count(): 计算列表中给定元素的总出现次数。 语法:
    List.count(element)

    List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]
    print ( List .count( 1 ))

    
    

    输出:

    4
    
  • 长度: 计算列表的总长度。 语法:
    len(list_name)

    List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]
    print ( len ( List ))

    
    

    输出:

    10
    
  • 索引(): 返回第一次出现的索引。开始和结束索引不是必需的参数。 语法:
    List.index(element[,start[,end]])

    List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]
    print ( List .index( 2 ))

    
    

    输出:

    1
    

    另一个例子:

    List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]
    print ( List .index( 2 , 2 ))

    
    

    输出:

    4
    

    List = [ 1 , 2 , 3 , 1 , 2 , 1 , 2 , 3 , 2 , 1 ]
    """index(element, start, end) : It will calculate till index end-1. """
    # will check from index 2 to 4.
    print ( "After checking in index range 2 to 4" )
    print ( List .index( 2 , 2 , 5 ))
    # will check from index 2 to 3.
    print ( "After checking in index range 2 to 3" )
    print ( List .index( 2 , 2 , 4 ))

    
    

    输出:

    After checking in index range 2 to 4
    4
    After checking in index range 2 to 3
    Traceback (most recent call last):
      File "", line 1, in 
        List.index(2,2,4)
    ValueError: tuple.index(x): x not in tuple
    
  • min(): 计算列表中所有元素的最小值。 语法:
    min(List)

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    print ( min ( List ))

    
    

    输出:

    1.054
    
  • max(): 计算列表中所有元素的最大值。 语法:
    max(List)

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    print ( max ( List ))

    
    

    输出:

    5.33
    

sort()和reverse()函数

  • 反向() 按升序对给定的数据结构(元组和列表)排序。Key和reverse_flag不是必需的参数,如果没有通过sorted()传递任何内容,reverse_flag将设置为False。 语法:
    sorted([list[,key[,Reverse_Flag]]])
     list.sort([key,[Reverse_flag]])

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    #Reverse flag is set True
    List .sort(reverse = True )
    #List.sort().reverse(), reverses the sorted list
    print ( List )

    
    

    输出:

    [5.33, 4.445, 3, 2.5, 2.3, 1.054]
    

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    sorted ( List )
    print ( List )

    
    

    输出:

    [1.054, 2.3, 2.5, 3, 4.445, 5.33]
    

删除列表元素

要删除一个或多个元素,即删除一个元素,可以使用许多内置函数,例如pop()&remove()和关键字,例如del。

  • pop(): 索引不是必需的参数,如果未提及,则取最后一个索引。 语法:
     list.pop([index])

    注意:索引必须在列表的范围内,否则会出现索引器。

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    print ( List .pop())

    
    

    输出:

    2.5
    

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    print ( List .pop( 0 ))

    
    

    输出:

    2.3
    
  • del(): 使用列表名和索引提到要删除的元素。 语法:
    del list.[index]

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    del List [ 0 ]
    print ( List )

    
    

    输出:

    [4.445, 3, 5.33, 1.054, 2.5]
  • 删除(): 要删除的元素使用列表名和元素来表示。 语法:
     list.remove(element)

    List = [ 2.3 , 4.445 , 3 , 5.33 , 1.054 , 2.5 ]
    List .remove( 3 )
    print ( List )

    
    

    输出:

    [2.3, 4.445, 5.33, 1.054, 2.5]
    

本文由 皮尤斯门战 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享