Python - 第二次复制一个字符时删除它 [英] Python - Remove a character the second time it is duplicated
问题描述
我想从字符串中删除一个,"(逗号),但只有第二次出现逗号,因为它需要采用正确的格式进行反向地理编码...
例如,我在 python 中有以下字符串:
43,14,3085
我将如何将其转换为以下格式:
43,143085
我尝试过使用 regex 和 str.split() 但还没有达到效果..
如果您确定该字符串只包含两个逗号,而您想删除最后一个,您可以使用 rsplit
和 join
:
在上面的 rsplit
中,从作为第二个参数给出的结束次数开始拆分:
然后使用join
将各个部分组合在一起:
I'm looking to remove a ',' (comma) from a string, but only the second time the comma occurs as it needs to be in the correct format for reverse geocoding...
As an example I have the following string in python:
43,14,3085
How would I convert it to the following format:
43,143085
I have tried using regex and str.split() but have not achieved result yet..
If you're sure that string only contains two commas and you want to remove the last one you can use rsplit
with join
:
>>> s = '43,14,3085'
>>> ''.join(s.rsplit(',', 1))
'43,143085'
In above rsplit
splits starting from the end number of times given as a second parameter:
>>> parts = s.rsplit(',', 1)
>>> parts
['43,14', '3085']
Then join
is used to combine the parts together:
>>> ''.join(parts)
'43,143085'
这篇关于Python - 第二次复制一个字符时删除它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!