这是列表内的字符串示例
item_s=["hello - this","is - a","sample - task","i","want - to","do"]
我可以有这样的输出:
item_s=["hello-this","is-a","sample-task","i","want-to","do"]
这是我尝试修剪空间的代码,但显示错误
amount=list(df["Price Range"])
def add_commas(s):
s1, s2 = s.split('-')
def process(s_part):
n, tmp_s = len(s_part), []
for i in range(n):
if i and not i % 3:
tmp_s.append(',')
tmp_s.append(s_part[n - i - 1])
return ''.join(tmp_s[::-1])
return process(s1) + '-' + process(s2)
def main():
amount=list(df["Price Range"])
return [add_commas(v) for v in amount]
main
您可以replace()
在列表理解中使用:
item_s=["hello - this","is - a","sample - task","i","want - to","do"]
item_s=[item.replace(" ", "") for item in item_s]
print(item_s)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句