python: convert “5,4,2,4,1,0” into [[5, 4], [2, 4], [1, 0]]

后端 未结 11 1077
梦如初夏
梦如初夏 2020-11-28 15:33

Is there a \"straightforward\" way to convert a str containing numbers into a list of [x,y] ints?

# from: \'5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5\'
# to: [[5, 4         


        
11条回答
  •  温柔的废话
    2020-11-28 15:44

    It may be interesting to have a generator. Here's a generator expression:

    import re
    ch = '5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5'
    genexp = ( map(int,ma.groups()) for ma in re.finditer('(\d+)\s*,\s*(\d+)',ch) )
    

提交回复
热议问题