Python的字符串,list,tuple,set,字典操作详解
2017-04-06 17:11
806 查看
1.字符串 python是要创建成字符串的元素,其中的每个字母都是单一的子串,把它放在' '单引号或是'' ''引号中,就完成了python 字符串的创建。 #str强制转换 >>> a=123 >>> b=str(a) #将整数转化为字符串 >>> b '123' >>> a=[1,2,3] >>> b=str(a) #将list转化为字符串 >>> b '[1, 2, 3]' #字符串下标: >>> a='abcdef' >>> a[1] #从0开始 'b' >>> a[-1] #从后往前的下标是从-1开始 'f' 如何修改和删除python字符串? 字符串和数字一样都是不可变数据类型,不能进行修改操作。要想修改或是删除原有字符串数据,最好的方法是重新创建一个字符串。 2.List 可以把python中的list列表理解为任意对像的序列,只要把需要的参数值放入到中括号[ ]里面就可以了,比如 >>> a=['aa','b',1,['hello','python']] >>> a ['aa', 'b', 1, ['hello', 'python']] #修改list中的值: >>> a=[1,2,3,4] >>> a[0]=9 >>> a [9, 2, 3, 4] #list的其他操作 >>> del a[1] >>> a [9, 3, 4] >>> a.sort() #注意,list的函数都是原地操作 >>> a [3, 4, 9] >>> a.append(8) #向list添加元素 >>> a [3, 4, 9, 8] >>> len(a) 4 >>> a.extend([4,5,6]) >>> a [3, 4, 9, 8, 4, 5, 6] >>> a.insert(0,7) #在第0个位置插入7,原来的元素往后挪 >>> a [7, 3, 4, 9, 8, 4, 5, 6] #列表索引切片操作 >>> b ['a', 'b', 'c', 'd', 'e', 'f'] >>> b[1:3] ['b', 'c'] 3.tuple >>> a=tuple('abcdef') >>> a ('a', 'b', 'c', 'd', 'e', 'f') >>> a[1:3] #元组切片操作 ('b', 'c') >>> b=list(a) >>> b ['a', 'b', 'c', 'd', 'e', 'f'] 元组的其他操作和list类似 4.set python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差) 下面列举一下常用的集合操作: 创建set >>> a=set('aabbcc') #set将字符串拆开,形成set >>> a #并且set最基本的特性就是不能重复 {'b', 'a', 'c'} #添加删除操作 >>> a.add('python') >>> a {'b', 'a', 'c', 'python'} >>> a.remove('python') >>> a {'b', 'a', 'c'} >>> a.update('python') #update操作会将传入的元素拆分为字符,组成set元素 >>> a {'h', 'n', 't', 'b', 'p', 'c', 'o', 'a', 'y'} #set的交、并、差集操作 >>> a=set('abc') >>> b=set('cdef') >>> a & b {'c'} >>> a | b {'a', 'd', 'e', 'b', 'f', 'c'} >>> a - b {'b', 'a'} #另外,set还可以直接进行比较 >>> a!=b True >>> b=set('abc') >>> a==b True >>> 'f' in b False >>> 'a' in b True 5.字典 字典的组成:字典是由大括号{ }来包含其数据的,大括号内包含键和其对应的值,一对键和值成为一个项。键和值用冒号:隔开,项和项之间用逗号,号隔开。空字典就是不包含任何项的大括号,像{ }这样就是一个空字典。 >>> a={'name':'zhouwentao','age':22,'xingbie':'nan'} #创建一个字典 >>> a {'name': 'zhouwentao', 'age': 22, 'xingbie': 'nan'} >>> a['age']=23 #修改字典中的值 >>> a {'name': 'zhouwentao', 'age': 23, 'xingbie': 'nan'} >>> a['school']='cqupt' #添加一个字典 >>> a {'name': 'zhouwentao', 'school': 'cqupt', 'age': 23, 'xingbie': 'nan'} >>> del a['school'] #删除一个字典的值 >>> a {'name': 'zhouwentao', 'age': 23, 'xingbie': 'nan'}
相关文章推荐
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python中的集合详解(列表List,元组Tuple,字典,set)
- 【Python】基础学习——列表(list),元祖(tuple),字典(dict),字符串(string),集合(set)
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python中的列表(list),字典(dict),字符串(str)的基本操作
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python的列表(List),元组(Tuple),集合(Set),字典(Dict)
- python的dict,set,list,tuple应用详解
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python教程学习简记1--Python数据类型和变量 字符串和编码 list和tuple 条件判断 循环 dict和set
- Python3学习(一)-基础、数据类型、变量、字符串和编码、list&tuple、if、for、while、dict、set、函数与参数
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- [转载] Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python 列表list 元组tuple 字典dict 集合set 介绍
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结