parse string of integer sets with intervals to list

后端 未结 6 2064
南旧
南旧 2021-02-09 13:47

I have \"2,5,7-9,12\" string.

I want to get [2, 5, 7, 8, 9, 12] list from it.

Is there any built-in function for it in python?

Thanks.

6条回答
  •  花落未央
    2021-02-09 14:11

    Not that I'm aware of, but you can easily make your own:

    1. Create a results list.
    2. Split strings by , and start iterating over the result.
      1. If the current string contains a - append a range to the list.
      2. If the current string is a number, append it to the list.
      3. Else return an error.
    3. Return the list.

提交回复
热议问题