溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python字符串數(shù)組互轉(zhuǎn)問題如何解決

發(fā)布時間:2023-02-24 10:04:46 來源:億速云 閱讀:109 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了python字符串數(shù)組互轉(zhuǎn)問題如何解決的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇python字符串數(shù)組互轉(zhuǎn)問題如何解決文章都會有所收獲,下面我們一起來看看吧。

    字符串轉(zhuǎn)list數(shù)組

    str = '1,2,3'
    arr = str.split(',')

    gpu_ids分配

    name = opt.name
    gpu_ids =[ int(item) for item in opt.gpu_ids.split(',')]
    # set gpu ids
    if len(gpu_ids) > 0:
        torch.cuda.set_device(gpu_ids[0])

    list數(shù)組轉(zhuǎn)字符串

    字符串類型list:

    arr = ['a','b']
    str = ','.join(arr)

    數(shù)字型list:

    arr = [1,2,3]
     
    str = ','.join(str(i) for i in b)

    二維list數(shù)組轉(zhuǎn)string:

    先轉(zhuǎn)numpy數(shù)組,再遍歷轉(zhuǎn)str:

    import os
    import numpy as np
     
    centroids= [[1,2],[3,4]]
    centroids=np.asarray(centroids)
    anchors = centroids.copy()
    widths = anchors[:, 0]
    sorted_indices = np.argsort(widths)
     
    out_string=""
    for i in sorted_indices:
        out_string += str(int(anchors[i, 0] * 416)) + ',' + str(int(anchors[i, 1] * 416)) + ', '
     
    print("str", out_string[:-2])

    延伸閱讀:python中的字符數(shù)字之間的轉(zhuǎn)換函數(shù)

    int(x [,base ])         將x轉(zhuǎn)換為一個整數(shù)    

    long(x [,base ])        將x轉(zhuǎn)換為一個長整數(shù)    

    float(x )               將x轉(zhuǎn)換到一個浮點數(shù)    

    complex(real [,imag ])  創(chuàng)建一個復(fù)數(shù)    

    str(x )                 將對象 x 轉(zhuǎn)換為字符串    

    repr(x )                將對象 x 轉(zhuǎn)換為表達式字符串    

    eval(str )              用來計算在字符串中的有效Python表達式,并返回一個對象    

    tuple(s )               將序列 s 轉(zhuǎn)換為一個元組    

    list(s )                將序列 s 轉(zhuǎn)換為一個列表    

    chr(x )                 將一個整數(shù)轉(zhuǎn)換為一個字符    

    unichr(x )              將一個整數(shù)轉(zhuǎn)換為Unicode字符    

    ord(x )                 將一個字符轉(zhuǎn)換為它的整數(shù)值    

    hex(x )                 將一個整數(shù)轉(zhuǎn)換為一個十六進制字符串    

    oct(x )                 將一個整數(shù)轉(zhuǎn)換為一個八進制字符串   

    chr(65)='A'

    ord('A')=65

    int('2')=2;

    str(2)='2'

    關(guān)于“python字符串數(shù)組互轉(zhuǎn)問題如何解決”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“python字符串數(shù)組互轉(zhuǎn)問題如何解決”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI