[英]How to remove whitespace in a list
我無法刪除列表中的空格。
invoer = "5-9-7-1-7-8-3-2-4-8-7-9"
cijferlijst = []
for cijfer in invoer:
cijferlijst.append(cijfer.strip('-'))
我嘗試了以下操作,但不起作用。 我已經從字符串中列出了一個列表,並分隔了所有內容,但"-"
現在變成了""
。
filter(lambda x: x.strip(), cijferlijst)
filter(str.strip, cijferlijst)
filter(None, cijferlijst)
abc = [x.replace(' ', '') for x in cijferlijst]
試試看:
>>> ''.join(invoer.split('-'))
'597178324879'
如果要使用不帶-
字符串中的數字,請使用.replace()
作為:
>>> string_list = "5-9-7-1-7-8-3-2-4-8-7-9"
>>> string_list.replace('-', '')
'597178324879'
如果要將數字作為數字list
,請使用.split()
:
>>> string_list.split('-')
['5', '9', '7', '1', '7', '8', '3', '2', '4', '8', '7', '9']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.