如何在Python中按结尾拆分带限制的字符串
我有以下字符串:如何在Python中按结尾拆分带限制的字符串,python,string,split,Python,String,Split,我有以下字符串: “hello.world.foo.bar” 我想拆分(以“”作为分隔符,只想得到从末尾开始的两个元素),如下所示: [“hello.world.foo”,“bar”] 我怎样才能做到这一点?是否在末尾存在限制?使用指定maxsplit(第二个参数)作为1: >>> "hello.world.foo.bar".rsplit('.', 1) # <-- 1: maxsplit ['hello.world.foo', 'bar'] >>“hello.worl
“hello.world.foo.bar”
我想拆分(以“”
作为分隔符,只想得到从末尾开始的两个元素),如下所示:
[“hello.world.foo”,“bar”]
我怎样才能做到这一点?是否在末尾存在限制?使用指定maxsplit(第二个参数)作为1
:
>>> "hello.world.foo.bar".rsplit('.', 1) # <-- 1: maxsplit
['hello.world.foo', 'bar']
>>“hello.world.foo.bar”.rsplit('.',1)#那么,split从左侧开始,rsplit从右侧开始?string.split('.',1)和string.rsplit('.',1)?@James,你说得对。在不指定第二个参数的情况下,对于string.split('.')
和string.rsplit('.')
,您将获得['hello'、'world'、'foo'、'bar']
。感谢您的确认