2016-12-04 210 views
-2

['0-0-0','1-10-20','3-10-15','2-30-20','1-0- 5','1-10-6','3-10-30','3-10-4'] 我怎样才能删除数字之间的所有连字符? 谢谢(我是小菜鸟)如何从字符串列表中删除连字符

+1

['str.replace'](https://docs.python.org/3/library/stdtypes.html#str.replace)例如。你的问题似乎过于通用。你有没有先尝试解决它? – poke

+3

看看答案[这里](http://stackoverflow.com/questions/22187233/how-to-delete-all-instances-of-a-character-in-a-string-in-python)。 –

+4

StackOverflow上已经有无数类似的问题。你的第一步应该是阅读文档(你会发现许多字符串方法)。在发布新问题之前,您的下一步是查看是否已经提出了这样的问题。 –

回答

2

你可以迭代一个for循环,并用空白替换连字符的每个实例。

hyphenlist = ['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4'] 
newlist = [] 

for x in hyphenlist: 
    newlist.append(x.replace('-', '')) 

此代码应该给你一个没有连字符的新列表。

1

或列表理解:

>>>l=['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4'] 
>>>[i.replace('-','') for i in l] 
['000', '11020', '31015', '23020', '105', '1106', '31030', '3104']