In Python, I'll introduce some built-in functions and standard libraries that are a little useful to know! There are various types from major to niche, but if you don't know, please check it out.
all () returns True if all the elements of the argument are True, and any () returns True if any one of the elements of the argument is True.
In [1]: all(_ % 2 == 0 for _ in [1, 2, 3]) #All even or not
Out[1]: False
In [2]: any(_ % 2 == 0 for _ in [1, 2, 3]) #Any one is even or not
Out[2]: True
divmod
It asks for the quotient and the remainder at once.
In [1]: 10 // 3, 10 % 3 #Seeking quotient and remainder
Out[1]: (3, 1)
In [2]: divmod(10, 3) #Exactly the same as above
Out[2]: (3, 1)
collections
Counter.most_common
It sorts in iterable in descending order of appearance frequency. You can instantly find the word that is used most in a sentence.
In [1]: from collections import Counter
In [2]: Counter(['a', 'b', 'c', 'a', 'b', 'a']).most_common()
Out[2]: [('a', 3), ('b', 2), ('c', 1)]
namedtuple
Create a value object. This is convenient when you want to have multiple attributes.
In [1]: from collections import namedtuple
In [2]: Doc = namedtuple('Doc', 'tf idf')
In [3]: doc = Doc(tf=0.1, idf=0.01)
In [4]: doc.tf, doc.idf
Out[4]: (0.1, 0.01)
OrderedDict
Normal dict does not maintain the order of insertion, but OrderedDict does.
In [1]: from collections import OrderedDict
In [2]: d = OrderedDict((('a', 1), ('b', 2), ('c', 3))) # OrderedDict(a=1, b=2, c=3)Please note that it will not be maintained
In [3]: d['d'] = 4 #Added at the end
In [4]: for k, v in d.iteritems(): print k, v #Print in the order of insertion
a 1
b 2
c 3
d 4
math
fsum
Calculates the sum of the values in iterable without losing digits.
In [1]: import math
In [2]: sum([.1] * 10) #It will drop digits
Out[2]: 0.9999999999999999
In [3]: math.fsum([.1] * 10)
Out[3]: 1.0
log10
For common logarithm, it is more accurate to use log10 (x) instead of log (x, 10).
In [1]: import math
In [2]: math.log(5, 10)
Out[2]: 0.6989700043360187
In [3]: math.log10(5) #Same as above, but with higher accuracy
Out[3]: 0.6989700043360189
fileinput
Multiple files can be processed at once. If no file is specified, the standard input is read.
abc.txt
a b c
def.txt
d e f
fileinput.py
import fileinput
for l in fileinput.input():
print l
$ python fileinput.py abc.txt def.txt #Read multiple files at once
a b c
d e f
$ cat abc.txt | python fileinput.py #Standard input
a b c
ConfigParser
Reads the config file and parses it nicely.
.gitconfig(part)
[user]
name = Hoge
email = [email protected]
[color]
ui = auto
diff = auto
status = auto
interactive = auto
branch = auto
grep = auto
configparser.py
import ConfigParser
cfg = ConfigParser.SafeConfigParser()
cfg.read('.gitconfig')
for sec in cfg.sections():
print '[{}]'.format(sec)
for opt in cfg.options(sec):
print '{} : {}'.format(opt, cfg.get(sec, opt))
$ python configparser.py
[user]
name : Hoge
email : [email protected]
[color]
ui : auto
diff : auto
status : auto
interactive : auto
branch : auto
grep : auto
shlex.split
It parses words and divides them nicely. Unlike str.split, it unifies the parts enclosed in quotation marks.
In [1]: import shlex
In [2]: '''he said 'you are beautiful!' '''.split() # ' 'The inside is divided
Out[2]: ['he', 'said', "'you", 'are', "beautiful!'"]
In [3]: shlex.split('''he said 'you are beautiful!' ''') # ' 'The inside is organized!
Out[3]: ['he', 'said', 'you are beautiful!']
Recommended Posts