This article is the 23rd day of MDC Advent Calendar 2020. December 23, 2020 is a weekday. e? Did you stop talking about prime numbers? Yes, I wonder if I'm getting tired of it (by the way, the prime number is 20201227 most recently). Did you know about weekdays? Oh, that's rude.
This is the n selection series that we will send you again. Now about Python. Moreover, it has been reduced from 5 to 2. What is "selection" anymore? When I recovered my Mac the other day, if I updated the Python version to the latest version, the tension increased a little, so I will write it with that glue. I was happy from my point of view, so I think that some people may feel joy in something different, but I would like it to be individual differences.
A method has been added that excludes the first or last string of the string.
In the past, using regular expressions would have been a quick way to do it, but you can easily do it with removeprefix ()
or removesuffix ()
.
>>> sample = 'test_sample_string'
>>> sample
'test_sample_string'
>>> import re
>>> re.sub('^' + re.escape('test_'), '', sample)
'sample_string'
>>> re.sub(re.escape('_string') + '$', '', sample)
'test_sample'
Yes, it's just like a regular expression.
>>> sample = 'test_sample_string'
>>> sample
'test_sample_string'
>>> sample.removeprefix('test_')
'sample_string'
>>> sample.removeprefix('test_sample_')
'string'
#Needless to say, if you meet the condition of the first character string, you can go anywhere.
>>> sample.removeprefix('sample_')
'test_sample_string'
#Must match as the first string
>>> sample.removesuffix('_string')
'test_sample'
>>> sample.removesuffix('test')
'test_sample_string'
#Similar to prefix
By the way, I also tried Japanese. You did it.
>>> sample2 = 'Red pajamas blue pajamas yellow pajamas'
>>> sample2
'Red pajamas blue pajamas yellow pajamas'
>>> sample2.removeprefix('Red pajamas')
'Blue pajamas yellow pajamas'
>>> sample2.removesuffix('Yellow pajamas')
'Red pajamas blue pajamas'
The only way to combine two dictionaries was to prepare a new dictionary to combine and then do your best with update (), but from this time it seems that it is possible with operators.
Specifically, it seems that you can easily do it with dic1 | dic2
.
>>> airport1 = {'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport2 = {'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport = airport1.copy()
>>> airport
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport.update(airport2)
>>> airport
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
Hmmm, it's really annoying ...
>>> airport1 = {'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport2 = {'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport1 | airport2
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
#Combine two dictionaries
>>> airport = airport1 | airport2
#Of course, I'll put it in a new dictionary
>>> airport
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport['NRT']
'Tokyo'
Sober|
Of the assignment operator corresponding to|=
It also supports.
>>> airport1
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya'}
>>> airport2
{'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
>>> airport1 |= airport2
>>> airport1
{'HND': 'Tokyo', 'KIX': 'Osaka', 'NGO': 'Nagoya', 'NRT': 'Tokyo', 'ITM': 'Osaka', 'CTS': 'Sapporo'}
It seems that it can be used when turning a for statement.
By the way, when the key is covered, it seems that it is prioritized to specify it later ( dic2
in dic1 | dic2
) as it was realized by update.
>>> code1 = {'Tokyo': 'HND', 'Osaka': 'KIX', 'Nagoya': 'NGO'}
>>> code2 = {'Tokyo': 'NRT', 'Osaka': 'ITM', 'CTS': 'Sapporo'}
>>> code1 | code2
{'Tokyo': 'NRT', 'Osaka': 'ITM', 'Nagoya': 'NGO', 'CTS': 'Sapporo'}
>>> code2 | code1
{'Tokyo': 'HND', 'Osaka': 'KIX', 'CTS': 'Sapporo', 'Nagoya': 'NGO'}
It's a familiar bonus every time (it seems that there are some tsukkomi that are not so familiar).
git clone git://github.com/yyuu/pyenv-update.git ${PYENV_ROOT}/plugins/pyenv-update
If you put in the plug-in, it was OK ... I didn't know it for a long time ... (I did a git pull every time).
If you look at the Release Notes, there are many more. How limited my use is ... Then Enjoy Python!
Recommended Posts