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!
  • 相关阅读:
    fatal error LNK1123: failure during conversion to COFF: file invalid or corr
    BEGIN_SINK_MAP(CMainDlg) SINK_ENTRY(IDC_EXPLORER1, ..。响应不到的
    第三周项目3-程序的多文件组织
    第三周项目2-三角形类(二)
    第三周项目1-三角形类(一)
    第三周课后实践-阅读程序
    第二周项目4-图书馆的书
    第二周项目3-时间类
    第二周项目2-长方柱类
    第二周项目1-旱冰场造价
  • 原文地址:https://www.cnblogs.com/allenblogs/p/2029527.html
Copyright © 2011-2022 走看看