当前位置:网站首页>list and string conversion
list and string conversion
2022-08-09 07:05:00 【Anakin6174】
List and string are commonly used data types, and sometimes they need to be converted to each other;
A common operation:
ls3 = [3,47,5]st = str(ls3)print(st)new_list = list(st)print(type(new_list))print(new_list)#output""" [3, 47, 5] ['[', '3', ',', ' ', '4','7', ',', ' ', '5', ']'] """
It can be seen that it is easy to convert a list into a string. Conversely, parsing a string into a list is relatively troublesome, and the result of parsing is likely to be inconsistent with expectations;
Better way of handling:
1. List to string
Command: ''.join(list)
Among them, the quotation marks are the separators between characters, such as ",", ";", "\t", etc.
Such as:
list = [1, 2, 3, 4, 5]
''.join(list) The result is: 12345
','.join(list) The result is: 1,2,3,4,5
2. String to list
print list('12345')
Output: ['1', '2', '3', '4', '5']
print list(map(int, '12345'))
output: [1, 2, 3, 4, 5]
str2 = "123 sjhid dhi"
list2 = str2.split() #or list2 = str2.split(" ")
print list2
['123', 'sjhid', 'dhi']
str3 = "www.google.com"
list3 = str3.split(".")
print list3
['www', 'google', 'com']
Reference: https://www.cnblogs.com/anningwang/p/7627117.html
边栏推荐
- P6阿里机试题之2020 斐波那契数
- 常用测试用例设计方法之正交实验法详解
- The working principle of the transformer (illustration, schematic explanation, understand at a glance)
- RK3568商显版开源鸿蒙板卡产品解决方案
- SSL证书最长有效期13个月,还有必要一次申请多年吗?
- imageio读取.exr报错 ValueError: Could not find a backend to open `xxx.exr‘ with iomode `r`
- 虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
- codeforces Valera and Elections (这思维题是做不明白了)
- 高项 04 项目整体管理
- Distributed id generator implementation
猜你喜欢
随机推荐
MongDb query method
redis学习笔记
Mysql实操
2017.10.26模拟 b energy
【sqlite3】sqlite3.OperationalError: table addresses has 7 columns but 6 values were supplied
2022年7月小结
【修电脑】系统重装但IP不变后VScode Remote SSH连接失败解决
SSL证书最长有效期13个月,还有必要一次申请多年吗?
无重复的字符的最长子串
当酷雷曼VR直播遇上视频号,会摩擦出怎样的火花?
01 自然语言处理NLP介绍
事务总结
car-price-deeplearning-0411
Distributed id generator implementation
字节跳动笔试题2020 (抖音电商)
【Oracle 11g】Redhat 6.5 安装 Oracle11g
Better Scroll Y上下滚动无法上拉滚动解决办法
Zero shift of leetcode
makefile记录
【模板】树链剖分 P3384