python 字符串数组互转

目录

字符串转list数组

gpu_ids分配

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'

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python中,可以使用int()函数将字符串转换为数字。要将一个字符串数组中的每个字符串都转换为数字,可以首先使用split()函数将字符串数组拆分为单个字符串,然后使用int()函数将每个字符串转换为数字。例如,如果我们有一个名为arr的字符串数组,可以使用以下代码将其转换为数字数组: arr = ["1", "2", "3"] nums = [int(x) for x in arr] # nums现在是一个包含数字的数组[1, 2, 3] 在这个例子中,我们使用列表推导式将arr中的每个字符串转换为数字,并将结果存储在新的nums数组中。通过这种方法,我们可以将字符串数组转换为数字数组。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python数组字符串互转详细操作](https://blog.csdn.net/mycocoo/article/details/115213445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python字符串数组--转换为-->数字型数组](https://blog.csdn.net/Erudite_x/article/details/119614955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值