亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用翻譯和maketrans功能從列表中刪除標點符號

使用翻譯和maketrans功能從列表中刪除標點符號

胡說叔叔 2022-08-11 20:23:55
需要從列表中刪除標點符號,然后將其保存在同一列表中法典:sentences = ["Hi! How are you doing?","Hope everything is fine.","Have an amazing day!"]type(list1)sentences = sentences.translate(str.maketrans('', '', string.punctuation))錯誤:AttributeError                            Traceback (most recent call last)<ipython-input-4-7b3b0cbf9c58> in <module>()      1 sentences = ["Hi! How are you doing?","Hope everything is fine.","Have an amazing day!"]      2 type(list1)----> 3 sentences = sentences.translate(str.maketrans('', '', string.punctuation))AttributeError: 'list' object has no attribute 'translate'
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

錯誤告訴你真相。列表沒有屬性,字符串有屬性。您需要對列表中的字符串(而不是列表本身)調用此函數。列表理解對此有好處。在這里,您可以調用列表中的每個字符串:translatetranslate()


import string


sentences = ["Hi! How are you doing?","Hope everything is fine.","Have an amazing day!"]


trans = str.maketrans('', '', string.punctuation)


[s.translate(trans) for s in sentences]

# ['Hi How are you doing', 'Hope everything is fine', 'Have an amazing day']


查看完整回答
反對 回復 2022-08-11
  • 1 回答
  • 0 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號