日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

最新python?字符串數組互轉問題_python

作者:AI視覺網奇 ? 更新時間: 2023-06-05 編程語言

字符串轉list數組

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數組轉字符串

字符串類型list:

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

數字型list:

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

二維list數組轉string:

先轉numpy數組,再遍歷轉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中的字符數字之間的轉換函數

int(x [,base ]) ? ? ? ? 將x轉換為一個整數 ? ?

long(x [,base ]) ? ? ? ?將x轉換為一個長整數 ? ?

float(x ) ? ? ? ? ? ? ? 將x轉換到一個浮點數 ? ?

complex(real [,imag ]) ?創建一個復數 ? ?

str(x ) ? ? ? ? ? ? ? ? 將對象 x 轉換為字符串 ? ?

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

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

tuple(s ) ? ? ? ? ? ? ? 將序列 s 轉換為一個元組 ? ?

list(s ) ? ? ? ? ? ? ? ?將序列 s 轉換為一個列表 ? ?

chr(x ) ? ? ? ? ? ? ? ? 將一個整數轉換為一個字符 ? ?

unichr(x ) ? ? ? ? ? ? ?將一個整數轉換為Unicode字符 ? ?

ord(x ) ? ? ? ? ? ? ? ? 將一個字符轉換為它的整數值 ? ?

hex(x ) ? ? ? ? ? ? ? ? 將一個整數轉換為一個十六進制字符串 ? ?

oct(x ) ? ? ? ? ? ? ? ? 將一個整數轉換為一個八進制字符串 ??

chr(65)='A'

ord('A')=65

int('2')=2;

str(2)='2'

原文鏈接:https://blog.csdn.net/jacke121/article/details/80613561

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新