• python


    Table 3: Methods Provided by Lists in Python
    Method NameUseExplanation
    append alist.append(item) Adds a new item to the end of a list
    insert alist.insert(i,item) Inserts an item at the ith position in a list
    pop alist.pop() Removes and returns the last item in a list
    pop alist.pop(i) Removes and returns the ith item in a list
    sort alist.sort() Modifies a list to be sorted
    reverse alist.reverse() Modifies a list to be in reverse order
    del del alist[i] Deletes the item in the ith position
    index alist.index(item) Returns the index of the first occurrence of item
    count alist.count(item) Returns the number of occurrences of item
    remove alist.remove(item) Removes the first occurrence of item
    Table 4: Methods Provided by Strings in Python
    Method NameUseExplanation
    center astring.center(w) Returns a string centered in a field of size w
    count astring.count(item) Returns the number of occurrences of item in the string
    ljust astring.ljust(w) Returns a string left-justified in a field of size w
    lower astring.lower() Returns a string in all lowercase
    rjust astring.rjust(w) Returns a string right-justified in a field of size w
    find astring.find(item) Returns the index of the first occurrence of item
    split astring.split(schar) Splits a string into substrings at schar
    Table 5: Operations on a Set in Python
    Operation NameOperatorExplanation
    membership in Set membership
    length len Returns the cardinality of the set
    | aset otherset Returns a new set with all elements from both sets
    & aset otherset Returns a new set with only those elements common to both sets
    - aset otherset Returns a new set with all items from the first set not in second
    <= aset <= otherset Asks whether all elements of the first set are in the second
    Table 6: Methods Provided by Sets in Python
    Method NameUseExplanation
    union aset.union(otherset) Returns a new set with all elements from both sets
    intersection aset.intersection(otherset) Returns a new set with only those elements common to both sets
    difference aset.difference(otherset) Returns a new set with all items from first set not in second
    issubset aset.issubset(otherset) Asks whether all elements of one set are in the other
    add aset.add(item) Adds item to the set
    remove aset.remove(item) Removes item from the set
    pop aset.pop() Removes an arbitrary element from the set
    clear aset.clear() Removes all elements from the set
    Table 7: Operators Provided by Dictionaries in Python
    OperatorUseExplanation
    [] myDict[k] Returns the value associated with k, otherwise its an error
    in key in adict Returns True if key is in the dictionary, False otherwise
    del del adict[key] Removes the entry from the dictionary
    Table 8: Methods Provided by Dictionaries in Python
    Method NameUseExplanation
    keys adict.keys() Returns the keys of the dictionary in a dict_keys object
    values adict.values() Returns the values of the dictionary in a dict_values object
    items adict.items() Returns the key-value pairs in a dict_items object
    get adict.get(k) Returns the value associated with kNone otherwise
    get adict.get(k,alt) Returns the value associated with kalt otherwise
    Table 10: Additional formatting options
    ModifierExampleDescription
    number %20d Put the value in a field width of 20
    - %-20d Put the value in a field 20 characters wide, left-justified
    + %+20d Put the value in a field 20 characters wide, right-justified
    0 %020d Put the value in a field 20 characters wide, fill in with leading zeros.
    . %20.2f Put the value in a field 20 characters wide with 2 characters to the right of the decimal point.
    (name) %(name)d Get the value from the supplied dictionary using name as the key.

              

    >>> price = 24
    >>> item = "banana"
    >>> print("The %s costs %d cents"%(item,price))
    The banana costs 24 cents
    >>> print("The %+10s costs %5.2f cents"%(item,price))
    The     banana costs 24.00 cents
    >>> print("The %+10s costs %10.2f cents"%(item,price))
    The     banana costs      24.00 cents
    >>> itemdict = {"item":"banana","cost":24}
    >>> print("The %(item)s costs %(cost)7.1f cents"%itemdict)
    The banana costs    24.0 cents
    >>>
  • 相关阅读:
    2017暑期集训Day 4
    2017暑期集训Day 5
    2017暑期集训Day 3
    Codeforces Round #433
    校内集训(20170906)
    校内集训(20170903)
    培训补坑(day10:双指针扫描+矩阵快速幂)
    培训补坑(day8:树上倍增+树链剖分)
    培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)
    培训补坑(day5:最小生成树+负环判断+差分约束)
  • 原文地址:https://www.cnblogs.com/lixiang-/p/5656916.html
Copyright © 2020-2023  润新知