Python 2 : Tricks
Posted on 18/09/2018, in Infomation Technology, Python.I use this note for only tricks in Python (quick exercises solution also).
Check string
-
Check ASCII
is_ascii = lambda s: len(s) == len(s.encode())
-
Check there is any digit numbers?
has_digit = lambda s: any(char.isdigit() for char in s)
-
s.isupper()
,s.islower()
List
-
Delete all same element ‘a’ from list
arr = [x for x in arr if x != 'a']
Note that, if we only want to delete 1 element a, use
arr.remove('a')
-
Remove duplicates and only keep 1 of them: change to
set
:b = set(a)
wherea
is a list