zoukankan      html  css  js  c++  java
  • Python之List Methods

    List Methods

    Here are some other common list methods.

    • list.append(elem) -- adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
    • list.insert(index, elem) -- inserts the element at the given index, shifting elements to the right.
    • list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
    • list.index(elem) -- searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use "in" to check without a ValueError).
    • list.remove(elem) -- searches for the first instance of the given element and removes it (throws ValueError if not present)
    • list.sort() -- sorts the list in place (does not return it). (The sorted() function shown below is preferred.)
    • list.reverse() -- reverses the list in place (does not return it)
    • list.pop(index) -- removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).

    Notice that these are *methods* on a list object, while len() is a function that takes the list (or string or whatever) as an argument.

    FOR and IN

    Python's *for* and *in* constructs are extremely useful, and the first use of them we'll see is with lists. The *for* construct -- for var in list -- is an easy way to look at each element in a list (or other collection). Do not add or remove from the list during iteration.

    squares = [1, 4, 9, 16]
    sum
    = 0 for num in squares:
    sum
    += num print sum ## 30

    If you know what sort of thing is in the list, use a variable name in the loop that captures that information such as "num", or "name", or "url". Since python code does not have other syntax to remind you of types, your variable names are a key way for you to keep straight what is going on.

    The *in* construct on its own is an easy way to test if an element appears in a list (or other collection) -- value in collection -- tests if the value is in the collection, returning True/False.

    list = ['larry', 'curly', 'moe']
    if 'curly' in list:
    print 'yay'

    The for/in constructs are very commonly used in Python code and work on data types other than list, so should just memorize their syntax. You may have habits from other languages where you start manually iterating over a collection, where in Python you should just use for/in.

    You can also use for/in to work on a string. The string acts like a list of its chars, so for ch in s: print ch prints all the chars in a string.

    Work for fun,Live for love!
  • 相关阅读:
    微信小程序上传文件(报错处理方式)
    自绘 TreeDataView 控件
    C# 读取outlook 本地签名
    【英雄帖】FreeRedis 邀请您一起优化项目。
    (30)ASP.NET Core3.1 集成Apollo快速安装与使用
    C#引用fo-dicom读取dicom文件异常
    .NET 开源导入导出库 Magicodes.IE 2.5发布
    WinUI 3 Preview 3 发布了,再一次试试它的性能
    Aspose.Word for DotNet 定位到每页,画笔 移动到某页。
    .NET Core下好用的FTP框架 FluentFTP
  • 原文地址:https://www.cnblogs.com/allenblogs/p/2029527.html
Copyright © 2011-2022 走看看