Python資料型別轉換操作

fjie發表於2017-05-15

資料型別轉換的一些操作

  1. string表示數值的list列表轉為int/float型列表

    
    # python 3.5
    
    a = [`1`, `2`, `3`]
    b = list(map(int, a)) # 轉為整型
    c = list(map(float, a)) # 轉為浮點型
  2. text/string轉為numpy中ndarray型別

    
    # python 3.5, numpy 1.12
    
    
    # 資料輸入格式為數字以‘,’分隔
    
    import numpy as np
    
    line = `1,2,3,4,5`
    x = np.fromstring(line, dtype=int, sep=`,`)


相關文章