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!
  • 相关阅读:
    ES6语法异步转同步(小程序中测试)
    js 图片保存至手机相册
    js字符串中查看有没有在数组中的值有的话全部替换掉
    java.sql.SQLException: Access denied for user 'Administrator'@'localhost'
    <mvc:annotation-driven>新增标签
    SpingMVC之<mvc:annotation-driven/>标签
    DecimalFormat 的用法
    sui.js和workflow2.js内容详解
    mac地址和ip地址、子网掩码和默认网关
    MQTT 3 ——MQTT与Spring Mvc整合
  • 原文地址:https://www.cnblogs.com/allenblogs/p/2029527.html
Copyright © 2011-2022 走看看