Main
代码: 全选
if __name__ == '__main__': # Runs main() if file wasn't imported.
main()
代码: 全选
<list> = <list>[<slice>] # Or: <list>[from_inclusive : to_exclusive : ±step
代码: 全选
<list>.append(<el>;) # Or: <list> += [<el>]
<list>.extend(<collection>;) # Or: <list> += <collection>
代码: 全选
<list>.sort() # Sorts in ascending order.
<list>.reverse() # Reverses the list in-place.
<list> = sorted(<collection>;) # Returns a new sorted list.
<iter> = reversed(<list>;) # Returns reversed iterator.
代码: 全选
sum_of_elements = sum(<collection>;)
elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)]
sorted_by_second = sorted(<collection>, key=lambda el: el[1])
sorted_by_both = sorted(<collection>, key=lambda el: (el[1], el[0]))
flatter_list = list(itertools.chain.from_iterable(<list>;))
product_of_elems = functools.reduce(lambda out, el: out * el, <collection>;)
list_of_chars = list(<str>;)
- For details about sorted(), min() and max() see sortable.
- Module operator provides functions itemgetter() and mul() that offer the same functionality as lambda expressions above.
https://gto76.github.io/python-cheatsheet