Finding whether a string starts with one of a list's variable-length prefixes

后端 未结 11 2133
傲寒
傲寒 2021-01-01 12:16

I need to find out whether a name starts with any of a list\'s prefixes and then remove it, like:

if name[:2] in [\"i_\", \"c_\", \"m_\", \"l_\", \"d_\", \"t         


        
11条回答
  •  北海茫月
    2021-01-01 13:06

    A bit hard to read, but this works:

    name=name[len(filter(name.startswith,prefixes+[''])[0]):]
    

提交回复
热议问题