当前位置:网站首页>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
边栏推荐
- C语言的内置宏(定义日志宏)
- bzoj 5333 [Sdoi2018]荣誉称号
- 学习小笔记---机器学习
- MUV LUV EXTRA 2019CCPC Qinhuangdao Station J Question KMP
- cut命令的使用实例
- The AD in the library of library file suffix. Intlib. Schlib. Pcblib difference
- 高项 04 项目整体管理
- (本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
- 神经网络优化器
- XxlJobConfig distributed timer task management XxlJob configuration class, replace
猜你喜欢

【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

leetcode 之 70 爬楼梯问题 (斐波那契数)

分布式理论

搭载开源鸿蒙系统的嵌入式XM-RK3568工业互联方案

错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.

基于布朗运动的文本生成方法-LANGUAGE MODELING VIA STOCHASTIC PROCESSES

高项 04 项目变更管理

排序第一节——插入排序(直接插入排序+希尔排序)(视频讲解26分钟)

01 自然语言处理NLP介绍

(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
随机推荐
MUV LUV EXTRA 2019CCPC秦皇岛站J题 KMP
SIGINT, SIGKILL, SIGTERM signal difference, summary of various signals
ByteDance Interview Questions: Mirror Binary Tree 2020
Codeforces Round #359 (Div. 2) C. Robbers' watch 暴力枚举
字节跳动面试题之镜像二叉树2020
eyb:Redis学习(2)
Zero shift of leetcode
金九银十即将到来,求职套路多,面试指南我来分享~
Neural Network Optimizer
差分约束-图论
tianqf的解题思路
力扣 636. 函数的独占时间
【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
jvm线程状态
MVN 中配置flyway mysq
bzoj 5333 [Sdoi2018]荣誉称号
2022 年全球十大最佳自动化测试工具
重要消息丨.NET Core 3.1 将于今年12月13日结束支持
stm32定时器之简单封装
imageio读取.exr报错 ValueError: Could not find a backend to open `xxx.exr‘ with iomode `r`