python中数字列表转化为数字字符串的实例代码
作者:henanlion 时间:2021-04-30 02:46:45
1. python中数字组成的列表转化为字符串或者一串数字
f=[1,2,3,4]
num=len(f)
m='' #建立空字符串
for i in range(num):
x=str(f[i])
m=m+x #利用字符串叠加的方法
print(int(m))
print(m)
显示结果:1234
’1234‘
优化结果:
最新方法:用列表表达式:
[str(i) for i in f] 可以一步到位。
2.python中把数字组成的列表转化为字符串列表
f=[1,3,4]
new=[] #建立空列表
for i in f:
q='%d'%i #格式化占位符,如果是数字转字符就用%s占位即可
new.append(q)
print(new)
显示结果:['1', '3', '4']
以上可以优化如下:
f=[1,3,4]
print(list(map(str,f))) #一行代码即可实现
显示结果:['1', '3', '4']
如下图:
更新使用列表表达式:[str(i) for i in f] #一步到位
总结:数字列表转化成一串数字,可以用for循环加上字符相加的方法。 数字列表转字符串则可以用占位符格式化,添加新列表即可。
3.Python中把字符组成的列表转化为数字
采用join的方法:
num_list=['1','2','3']
str_list = ''.join(num_str) #把列表中的元素连起来
print(int(str_list))
结果显示:123
4.Python中把一串数字字符转化为单个字符列表或者数字列表
用list
a='123'
print(list(a))
显示结果:['1','2','3']
使用map映射函数:
b=['1','2','3']
list(map(int, b)) #把int作用到b中的每一个元素
显示结果:[1,2,3]
更新:列表表达式:[int(i) for i in a.split()]
5. Python中把字符串列表转化为数字列表
f=['1','2','3']
最新方法:用列表表达式:
[str(i) for i in f] 可以一步到位。
参考文章:
列表之间的数字与字符转化
来源:https://blog.csdn.net/henanlion/article/details/108691169
标签:数字列表,转化,数字字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一篇文章了解Python中常见的序列化操作
2023-01-22 05:22:20
Python快速生成随机密码超简单实现
2022-08-07 19:26:09
![](https://img.aspxhome.com/file/2023/3/77923_0s.png)
asp下为什么韩文字后面显示分号?
2011-03-10 11:07:00
Python实现查找系统盘中需要找的字符
2022-10-02 02:35:50
Python调用百度AI实现颜值评分功能
2023-07-30 22:53:40
![](https://img.aspxhome.com/file/2023/9/61439_0s.jpg)
python爬虫爬取淘宝商品信息(selenum+phontomjs)
2022-07-07 05:40:52
![](https://img.aspxhome.com/file/2023/9/72869_0s.jpg)
python实现批量修改图片格式和尺寸
2021-02-12 10:47:35
python pands实现execl转csv 并修改csv指定列的方法
2022-11-20 01:45:27
Python实现变声器功能(萝莉音御姐音)
2023-04-12 23:04:16
![](https://img.aspxhome.com/file/2023/0/96980_0s.jpg)
opencv调用yolov3模型深度学习目标检测实例详解
2022-04-30 19:37:55
![](https://img.aspxhome.com/file/2023/1/95201_0s.jpg)
让插入到 innerHTML 中的 javascript 跑起来
2008-07-01 13:14:00
pytorch 实现二分类交叉熵逆样本频率权重
2021-04-29 00:25:29
![](https://img.aspxhome.com/file/2023/5/90365_0s.png)
Django配置文件代码说明
2023-02-13 05:40:13
![](https://img.aspxhome.com/file/2023/7/99017_0s.png)
MYSQL各字段的长度是多少?
2009-10-28 18:32:00
Python一行代码实现自动发邮件功能
2021-04-06 06:04:38
![](https://img.aspxhome.com/file/2023/6/107016_0s.png)
Python合并多个Excel数据的方法
2022-11-02 06:31:33
![](https://img.aspxhome.com/file/2023/0/81820_0s.png)
dl+ol应用
2008-06-21 17:04:00
js友好的表单验证程序vform
2007-08-16 13:32:00
PHP attributes()函数讲解
2023-06-04 09:33:02
![](https://img.aspxhome.com/file/2023/0/55410_0s.png)
Python 打印中文字符的三种方法
2022-11-14 10:22:07