How can I remove multiple characters in a list?

后端 未结 5 894
天命终不由人
天命终不由人 2020-12-11 04:56

Having such list:

x = [\'+5556\', \'-1539\', \'-99\',\'+1500\']

How can I remove + and - in nice way?

This works but I\'m looking f

5条回答
  •  情歌与酒
    2020-12-11 05:36

    string.translate() will only work on byte-string objects not unicode. I would use re.sub:

    >>> import re
    >>> x = ['+5556', '-1539', '-99','+1500', '45+34-12+']
    >>> x = [re.sub('[+-]', '', item) for item in x]
    >>> x
    ['5556', '1539', '99', '1500', '453412']
    

提交回复
热议问题