Python基础学习笔记3 - list 和 str 的区别,转化,list解析
来源:互联网 发布:淘宝品牌的市场定位 编辑:程序博客网 时间:2024/06/06 03:22
list 和 str 的最大区别是:
list是可变的,str是不可变的
e.g list.append('test) #这个是允许的
teststr='test'teststr[1]='a'
返回TypeError: 'str' object does not support item assignment
区别2:list是多维的,
e.g
listMulti=[[1,2],[3,4]]
list和str的转换:
teststr='Hello I like Python'testList=teststr.split()print testListteststr='Hello, I like Python'testList=teststr.split(',')print testList
分别返回:['Hello', 'I', 'like', 'Python']
['Hello', ' I like Python']
split(...) S.split([sep [,maxsplit]]) -> list of stringsReturn a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splitsare done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed fromthe result.
join是split的相反方法
jointest=''.join(teststr)print jointest返回: Hello, I like Python
list的解析功能很有用(简洁优雅的Python):
求平方:
squares = [x**2 for x in range(1,10)]print squares
返回:[1, 4, 9, 16, 25, 36, 49, 64, 81]
求100以内能被3整除的数
aliquto = [n for n in range(1,100) if n%3==0]print aliquto
return [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
更优的实现: range(3,100,3)
0 0
- Python基础学习笔记3 - list 和 str 的区别,转化,list解析
- Python学习笔记:list,tuple和str
- 【学习笔记】Python基础-字典Dict和Set和List与Str扩展
- python中str.find和 in list的区别使用
- python学习-str list
- python中将一个全部为int的list 转化为str的list
- python学习----list和tuple的区别
- list和str的反转
- python学习之路-字符串(str)和列表(list)的互相转换
- python之list.append()和list.extend(list)的区别
- python之list.append()和list.extend(list)的区别
- python str 转换 list
- Python List转str
- python中str和list互相转换
- Python 基础学习笔记2(List 的 append , extend方法)
- python学习笔记--list基础命令
- [学习笔记]Python基础_列表List
- python学习(3)python基础、使用list和tuple
- java插入排序法例子
- model 新增 map 不存在字段 [NotMapped]
- linux基础(19)--linux磁盘加密--RHEL6.5
- 作业一
- Subsets(Array/ Bit Manipulation )
- Python基础学习笔记3 - list 和 str 的区别,转化,list解析
- 网络封包分析工具Charles(接口抓取)
- 位运算总结,&,|,^,~,>>,<<
- 冒泡法排序
- Servlet和JSTL在web.xml中的配置
- ExtJs4日期datefield控件设置默认值
- vb26
- 一些java中的官方API的应用场景
- Handler和HandlerThread简单研究