Python memo using perl --join

――I thought it was a python join, so I'll make a note of it.

Error when joining numbers

--This is an image of converting the date into a character string. "2016/4" is the value you want.

>>> ym = [2016,4]
>>> "/".join(ym)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found

--An error has occurred. You should concatenate with a character string.

Convert to string

--Convert to a string with map.

>>> ym = [2016,4]
>>> "/".join(map(lambda x:str(x),ym))
'2016/4'

――It's in the shape you want.

Alignment

--I want to fill the month part with 0. "2016/04" is the value you want.

>>> ym = [2016,4]
>>> "/".join(map(lambda x:(u"%02d"%x),ym))
'2016/04'

With perl ...

――Is it like this? If you don't fill 0, perl is easier.

% perl -le 'my @ym = (2016,4);print join("/",@ym)'
2016/4

% perl -le 'my @ym = (2016,4);print join("/",map {sprintf("%02d",$_)}@ym)'
2016/04

Recommended Posts

Python memo using perl --join
Python memo using perl --format is convenient.
Python memo
python memo
Python memo
python memo
Python memo
Python memo
Python memo
[Python] Memo dictionary
python beginner memo (9.1)
Start using Python
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
Scraping using Python
[Python] Numpy memo
[Personal memo] julia --Using Python library with julia using PyCall
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Python module (Python learning memo ④)
Operate Redmine using Python Redmine
Visualization memo by Python
Fibonacci sequence using Python
Data analysis using Python 0
Python test package memo
Data cleaning using Python
python regular expression memo
Binary search (python2.7) memo
[My memo] python -v / python -V
Python3 List / dictionary memo
Python Tips (my memo)
[Python] Memo about errors
Using Python #external packages
DynamoDB Script Memo (Python)
WiringPi-SPI communication using Python
Python basic memo --Part 2
python recipe book Memo
Age calculation using python
Basic Python command memo
Python OpenCV tutorial memo
Search Twitter using Python
Python basic grammar memo
TensorFlow API memo (Python)
Name identification using python
Notes using Python subprocesses
python useful memo links
Python decorator operation memo
Memo using trigonometric functions
Try using Tweepy [Python2.7]
Python basic memo --Part 1
Effective Python Memo Item 3
Divisor enumeration Python memo
[Heroku] Memo for deploying Python apps using Heroku on Windows [Python]
The pain of gRPC using Python. November 2019. (Personal memo)
Python memo using perl-Dictionary type (case is not valid)