当前位置:网站首页>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
边栏推荐
猜你喜欢
The working principle of the transformer (illustration, schematic explanation, understand at a glance)
(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
排序第三节——交换排序(冒泡排序+快速排序+快排的优化)(5个视频讲解)
常见的分布式事务解决方案
Variable used in lambda expression should be final or effectively final报错解决方案
错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
差分约束-图论
ByteDance Written Exam 2020 (Douyin E-commerce)
leetcode 之盛水问题
The water problem of leetcode
随机推荐
unity第一课
codeforces Valera and Elections (这思维题是做不明白了)
Better Scroll Y上下滚动无法上拉滚动解决办法
【Docker】Docker安装MySQL
集合内之部原理总结
字节也开始缩招了...
HDU - 3183 A Magic Lamp Segment Tree
事务总结
Singleton DCL (double check the lock) full han mode and the hungry
【转载】Deep Learning(深度学习)学习笔记整理
力扣第 305 场周赛复盘
failed (13: Permission denied) while connecting to upstream
顺序表删除所有值为e的元素
XxlJobConfig distributed timer task management XxlJob configuration class, replace
Unity first lesson
2017 G icpc shenyang Infinite Fraction Path BFS + pruning
tianqf的解题思路
MVN 中配置flyway mysq
Example of using the cut command
2019南昌网络赛 C题,Hello 2019