当前位置:网站首页>list与string转换
list与string转换
2022-08-09 06:56:00 【Anakin6174】
list与string是常用的数据类型,有时候需要相互转换;
一个常见的操作:
ls3 = [3,47,5]
st = str(ls3)
print(st)
new_list = list(st)
print(type(new_list))
print(new_list)
#输出
""" [3, 47, 5] <class 'list'> ['[', '3', ',', ' ', '4', '7', ',', ' ', '5', ']'] """
可见,将list转成string很容易,反过来讲string解析成list就相对麻烦,很可能解析的结果与预期不一致;
比较好的处理方式:
1、list转字符串
命令:’’.join(list)
其中,引号中是字符之间的分割符,如“,”,“;”,“\t”等等
如:
list = [1, 2, 3, 4, 5]
‘’.join(list) 结果即为:12345
‘,’.join(list) 结果即为:1,2,3,4,5
2、字符串转list
print list(‘12345’)
输出: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
print list(map(int, ‘12345’))
输出: [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’]
参考:https://www.cnblogs.com/anningwang/p/7627117.html
边栏推荐
- 买口罩(0-1背包)
- RK3568商显版开源鸿蒙板卡产品解决方案
- io.lettuce.core.RedisCommandTimeoutException Command timed out
- P7 Alibaba Interview Questions 2020.07 Sliding Window Algorithm (Alibaba Cloud Interview)
- XILINX K7 FPGA+RK3399 PCIE驱动调试
- 2017.10.26模拟 b energy
- crc计算
- idea中PlantUML插件使用
- 多米诺骨牌
- Output method of list string print(*a) print(““.join(str(c) for c in a) )
猜你喜欢
找不到和chrome浏览器版本不同的chromedriver的解决方法
安装flask
排序第三节——交换排序(冒泡排序+快速排序+快排的优化)(5个视频讲解)
Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
Built-in macros in C language (define log macros)
常见的分布式事务解决方案
Fragments
XILINX K7 FPGA+RK3399 PCIE驱动调试
INSTALL_RPATH and BUILD_RPATH problem in CMake
随机推荐
leetcode:55. 跳跃游戏
无重复的字符的最长子串
【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
Error: flask: TypeError: 'function' object is not iterable
XxlJobConfig distributed timer task management XxlJob configuration class, replace
makefile记录
crc计算
【Docker】Docker安装MySQL
Introduction and use of BeautifulSoup4
如何 认识与学习BASH
MongDb的查询方式
2022年7月小结
MVN 中配置flyway mysq
leetcode 之 零移位
stm32定时器之简单封装
力扣 636. 函数的独占时间
pycharm环境包导入到另外一个环境
Distributed id generator implementation
字节跳动笔试题2020 (抖音电商)
排序第二节——选择排序(选择排序+堆排序)(两个视频讲解)