codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Any better way for this removal? [typo correction]


On 2020-11-07 10:51, Tim Chase wrote:
>   from string import ascii_lowercase
>   text = ",".join(ascii_lowercase)
>   to_throw_away = 5

[derp]

For obvious reasons, these should be s/\<n\>/to_throw_away/g

To throw away the trailing N delimited portions:

>   new_string = text.rsplit(',', n)[0]

  new_string = text.rsplit(',', to_throw_away)[0]

and to remove the leading N fields

>   new_string = text.split(',', n)[-1]

  new_string = text.split(',', to_throw_away)[-1]

-tkc