翼度科技»论坛 编程开发 python 查看内容

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

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
字符串转list数组
  1. str = '1,2,3'
  2. arr = str.split(',')
复制代码
gpu_ids分配
  1. name = opt.name
  2. gpu_ids =[ int(item) for item in opt.gpu_ids.split(',')]
  3. # set gpu ids
  4. if len(gpu_ids) > 0:
  5.     torch.cuda.set_device(gpu_ids[0])
复制代码
list数组转字符串

字符串类型list:
  1. arr = ['a','b']
  2. str = ','.join(arr)
复制代码
数字型list:
  1. arr = [1,2,3]

  2. str = ','.join(str(i) for i in b)
复制代码
二维list数组转string:
先转numpy数组,再遍历转str:
  1. import os
  2. import numpy as np

  3. centroids= [[1,2],[3,4]]
  4. centroids=np.asarray(centroids)
  5. anchors = centroids.copy()
  6. widths = anchors[:, 0]
  7. sorted_indices = np.argsort(widths)

  8. out_string=""
  9. for i in sorted_indices:
  10.     out_string += str(int(anchors[i, 0] * 416)) + ',' + str(int(anchors[i, 1] * 416)) + ', '

  11. 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'
到此这篇关于最新python 字符串数组互转问题的文章就介绍到这了,更多相关python 字符串数组互转内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/article/276276.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具