Python delete in a string

后端 未结 4 1657
长情又很酷
长情又很酷 2021-01-21 08:51

I have these 3 strings:

YELLOW,SMALL,STRETCH,ADULT,T21fdsfdsfs
YELLOW,SMALL,STRETCH,ADULT,Tdsfs
YELLOW,SMALL,STRETCH,ADULT,TD

4条回答
  •  遇见更好的自我
    2021-01-21 08:57

    According to The Zen of Python:

    There should be one-- and preferably only one --obvious way to do it.

    ...so here's a third, which uses rpartition:

    >>> for item in catalogue:
    ...     print item.rpartition(',')[0]
    ... 
    YELLOW,SMALL,STRETCH,ADULT
    YELLOW,SMALL,STRETCH,ADULT
    YELLOW,SMALL,STRETCH,ADULT
    

    I haven't compared its performance against the previous two answers.

提交回复
热议问题