【发布时间】:2019-03-14 09:05:24
【问题描述】:
例如,我有以下字符串:
a-b-c-d-e-f
我想删除第一个元素和最后两个元素,所以输出将是:
b-c-d
我尝试使用rsplit,但输出是列表:
print string.rsplit('-',)[1:-2]
['b', 'c', 'd']
如何将列表元素组合回字符串?有没有更简单的方法来删除元素而不是使用rsplit?
【问题讨论】:
-
string = '-'.join(new_string) 其中new_string是拆分列表
-
谢谢大家。效果很好!
-
这里是 another website,它解释了 str.join,即使使用你的分隔符。
-
@gofvonx,我不认为这是一个骗局,仅仅是因为答案的方法是如此不同。这个问题没有函数知识,所以找到你引用的那个可能很困难。话虽如此。我认为在此处提问之前阅读一些围绕问题的language documentation 是有价值的。