最新python 字符串数组互转问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

最新python 字符串数组互转问题

AI视觉网奇   2023-03-20 我要评论

字符串转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'

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们