简单介绍最新python 字符串数组互转问题

Crq
Crq
管理员
1729
文章
0
粉丝
Linux教程评论172字数 407阅读1分21秒阅读模式
摘要这篇文章主要介绍了最新python 字符串数组互转问题,主要介绍了字符串转list数组问题和list数组转字符串问题,本文结合示例代码给大家介绍的非常详细,需要的朋友可以参考下
字符串转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'

到此这篇关于最新python 字符串数组互转问题的文章就介绍到这了

weinxin
我的微信
微信号已复制
我的微信
这是我的微信扫一扫
 
Crq
  • 本文由 Crq 发表于2025年1月22日 15:11:10
  • 转载请注明:https://www.cncrq.com/12718.html
让KVM虚拟机支持console功能 Linux教程

让KVM虚拟机支持console功能

在工作中,我们可能都会接触到 KVM 虚拟机,并且公司的很多应用也都会跑在 KVM 虚拟机上。因此,对 KVM的熟练应用,也是运维必不可少的能力之一。那么在 KVM 的实践过程中,...
在 RHEL 7.1 上设置 Mesos/Marathon 集群 Linux教程

在 RHEL 7.1 上设置 Mesos/Marathon 集群

Mesos 是一套分布式集群管理器,旨在通过以动态方式于不同任务之间共享资源的方式改进资源使用率;Marathon 是一套用于在 Mesos 之上运行长期运行应用程序或者服务的框架...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证