Useful to remember! 10 Python Standard Libraries

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.

Built-in functions

all and any

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

Useful to remember! 10 Python Standard Libraries
Python to remember only with hello, worlds
Python 3.9 dict merge (`|`) seems to be useful
Python3 standard input I tried to summarize
[Python] Add comments to standard input files
Updated to Python 2.7.9
[Python] Standard input
"Backport" to python 2
How to output "Ketsumaimo" as standard output in Python
List of Python code to move and remember
[CentOS8] How to output Python standard output to systemd log
Python: Use zipfile to unzip from standard input
You should know if you use Python! 10 useful libraries
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Python] Super useful debugging
Rewrite Python2 code to Python3 (2to3)
python decorator to retry
Introduction to Python language
Linux commands to remember
Python beginners talk about how to remember this much
Introduction to OpenCV (python)-(2)
[Python] Change standard input from keyboard to text file
Note to daemonize python
Introducing Python 2.7 to CentOS 6.6
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
[Python] About standard input
python useful memo links
[Python] It might be useful to list the data frames
Try to display various information useful for debugging with python
How to debug the Python standard library in Visual Studio
Change the standard output destination to a file in Python
To go back and forth between standard python, numpy, pandas ①
Five useful Python data types that are easy to forget
A standard way to develop and distribute packages in Python
Useful tricks related to list and for statements in Python
List of libraries to install when installing Python using Pyenv
Go language to see and remember Part 8 Call GO language from Python
Connect to BigQuery with Python
Post from Python to Slack
How to install Python [Windows]
Post to vim → Python → Slack
Introduction to Python Django (2) Win
To flush stdout in Python
Convert numpy int64 to python int
python3: How to use bottle (2)
[Python] Convert list to Pandas [Pandas]
Python 3.4 or later standard pip
Cheating from PHP to Python
A road to intermediate Python
[Python3] Standard input [Cheat sheet]
Try to understand Python self
Python standard unittest usage notes
Login to website in Python
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Post to slack with Python 3
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Python3 standard input (competition pro)