当前位置:网站首页>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
边栏推荐
- 【ROS2原理8】节点到参与者的重映射
- 2022年7月小结
- The water problem of leetcode
- SIGINT, SIGKILL, SIGTERM signal difference, summary of various signals
- 基于布朗运动的文本生成方法-LANGUAGE MODELING VIA STOCHASTIC PROCESSES
- 入门cv必读的10篇baseline论文
- rsync:recv_generator: mkdir (in backup) failed:Permission denied (13) |failed to set times on '.'
- 查看日志常用命令
- failed (13: Permission denied) while connecting to upstream
- Service
猜你喜欢
随机推荐
搭载开源鸿蒙系统的嵌入式XM-RK3568工业互联方案
Lottie系列四:使用建议
postgresql窗口功能
2022 年全球十大最佳自动化测试工具
car-price-deeplearning-0411
【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
数据一致性架构
2017 G icpc shenyang Infinite Fraction Path BFS + pruning
基于布朗运动的文本生成方法-LANGUAGE MODELING VIA STOCHASTIC PROCESSES
Service
字节也开始缩招了...
半导体新能源智能装备整机软件系统方案设计
买口罩(0-1背包)
HDU - 3183 A Magic Lamp 线段树
rsync:recv_generator: mkdir (in backup) failed:Permission denied (13) |failed to set times on '.'
什么是分布式事务
细谈VR全景:数字营销时代的宠儿
leetcode:55. 跳跃游戏
TCP段重组PDU
ByteDance Written Exam 2020 (Douyin E-commerce)