python杂记20180806

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lockey23/article/details/81461065
python buildin functions:

    https://www.programiz.com/python-programming/methods/built-in/abs

sorted() Parameters

sorted() takes two three parameters:

    iterable - sequence (string, tuple, list) or collection (set, dictionary, frozen set) or any iterator 
    reverse (Optional) - If true, the sorted list is reversed (or sorted in Descending order)
    key (Optional) - function that serves as a key for the sort comparison
>>> x='sdfq2aAFSDFasda'
>>> sorted(x)
['2', 'A', 'D', 'F', 'F', 'S', 'a', 'a', 'a', 'd', 'd', 'f', 'q', 's', 's']
>>> sorted(x,reverse=True)
['s', 's', 'q', 'f', 'd', 'd', 'a', 'a', 'a', 'S', 'F', 'F', 'D', 'A', '2']
>>> pySet = {'e', 'a', 'u', 'o', 'i'}
>>> sorted(pySet)
['a', 'e', 'i', 'o', 'u']
>>> sorted(pySet,reverse=True)
['u', 'o', 'i', 'e', 'a']
>>> sorted(pyDict)
['a', 'e', 'i', 'o', 'u']
>>> pyFSet = frozenset(('e', 'a', 'u', 'o', 'i'))
>>> sorted(pyFSet)
['a', 'e', 'i', 'o', 'u']


Sort the list using sorted() having a key function

>>> def takeSecond(elem):
...     return elem[1]
... 
>>> ran = [(2, 2), (3, 4), (4, 1), (1, 3)]
>>> sorted(ran, key=takeSecond)
[(4, 1), (2, 2), (1, 3), (3, 4)]

A list also has sort() method which performs the same way as sorted(). Only difference being, sort() method doesn't return any value and changes the original list itself.



any() and all()
如何删除以下列表中的数值0以及False值
>>> randomList = [1, 'a', 0, False, True, '0',None,None,0,False,90]
>>> filteredList = filter(None, randomList)
>>> filteredList
[1, 'a', True, '0', 90]


frozenset() for Dictionary
When you use dictionary as an iterable for a frozen set. It only takes key of the dictionary to create the set.

猜你喜欢

转载自blog.csdn.net/Lockey23/article/details/81461065