python杂记20180806

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值