python数据类型基本操作
目录
1、字符串.... 1
2、列表[ ] 3
3、元组 ( ) 4
4、字典 { } 4
5、SET集合... 7
1、字符串
1.1查找字符串
find查找
>>> msg = "what's your company's name?"
>>> msg.find('name')
22
>>> msg.find('s')
5
index跟find()方法一样,只不过如果str不在 string中会报一个异常.
>>> msg = "what's your company's name?"
>>> msg.index('name')
22
>>> msg.index('ndf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
1.2大小写转换
>>> msg = 'WangChuang'
>>> msg.upper() #转换 string 中的小写字母为大写
'WANGCHUANG'
>>> msg.lower() #转换 string 中所有大写字符为小写.
'wangchuang'
>>> msg.swapcase() #大小写互换
'wANGcHUANG'
1.3分割成列表
>>> msg = "what's your company's name?"
>>> msg.split()
["what's", 'your', "company's", 'name?']
>>> msg.split("'") #指定分隔符,默认按空格
['what', 's your company', 's name?']
1.4将列表转换成字符串
>>> msg_list = ['a','b','c']
>>> msg_list
['a', 'b', 'c']
>>> ''.join(msg_list)
'abc'
>>> ' '.join(msg_list) ##以空格分割
'a b c'
>>> '|'.join(msg_list) ##以|分割
'a|b|c'
1.5检查字符串开头和结尾
以什么值开头和以什么值结尾,是为true假为false
>>> a = 'bcd'
>>> a
'bcd'
>>> a.startswith('a') #检查开头
False
>>> a.startswith('b')
True
>>> a.endswith('d') #检查结尾
True
>>> a.endswith('c')
False
1.6替换字符串内容
>>> str = 'wangchuang'
>>> str.replace("wang","hello")
'hellochuang'三
1.7检查字符串开头和结尾
以什么值开头和以什么值结尾,是为true假为false
>>> a = 'bcd'
>>> a
'bcd'
>>> a.startswith('a') #检查开头
False
>>> a.startswith('b')
True
>>> a.endswith('d') #检查结尾
True
>>> a.endswith('c')
False
1.8替换字符串内容
>>> str = 'wangchuang'
>>> str.replace("wang","hello")
'hellochuang'
1.9统计字符串里某个字符出现的次数
>>> srt = 'ababab'
>>> srt.count('b')
3
2、列表[ ]
列表list:
- 查找和插入的时间随着元素的增加而增加
- 占用空间小,浪费内存很少
- 通过下标查询
- 有序
定义列表:
- name_list = ['a', 'b', 'c', 'd'] #直接定义
- list = [i for i in range(10)] #使用列表生成式
- def foo(x):
return x**x
list = [foo(i) for i in range(10)] #生成式里面还可以是函数哦
2.1查
>>> name_list[1]
'b'
>>> name_list[2]
'c'
>>> name_list.index('d') #取值对应的下标号
>>> name_list[1:4]
['b', 'c', 'd']
>>> name_list[1:3] #按范围取值
['b', 'c']
>>> name_list[-2:] #取最后两个值
['c', 'd']
>>> name_list[:2] #取前两个
['a', 'b']
2.2增
>>> name_list.append('e') #添加数据
>>> name_list
['a', 'b', 'c', 'd', 'e']
>>>
name_list.insert(2,'wang') ##指定位置插入数据
>>> name_list
['a', 'b', 'wang', 'c', 'd', 'e']
>>>
name_list.extend('12345') #每一个字符都当成一个元素,添加进去
>>> name_list
['a', 'b', 'd', 'e', '1', '2', '3',
'4', '5']
2.3删
>>>
name_list.remove('wang') #按值删除
>>> name_list
['a', 'b', 'c', 'd', 'e']
>>> name_list
['a', 'b', 'c', 'd', 'e']
>>> del name_list[2] #按下标删除
>>> name_list
['a', 'b', 'd', 'e']
2.4改
>>> name_list
['a', 'b', 'd', 'e']
>>>
name_list.reverse() #反转列表
>>> name_list
['e', 'd', 'b', 'a']
3、元组 ( )
元组只读,不可修改
>>> msg = ('a','b','c')
>>> msg[0]
'a'
>>> list(msg) #元组转换成列表
['a', 'b', 'c']
>>> tuple(a) #列表转换成元组
('a', 'b', 'c')
4、字典 { }
字典dict:
- 查找和插入的速度极快,不会随着key的增加而增加
- 需要占用大量的内存,内存浪费多
- Key不可变
- 默认无序
4.1字典增删改查
>>> dic =
{'a':1,'b':2,'name':'wangchuang'}
>>> dic['name'] #查看
'wangchuang'
>>> dic.get('name') #get查看,就算没有这个键,也不会报错
'wangchuang'
>>> dic['c'] = 3 #增加
>>> dic
{'a': 1, 'c': 3, 'b': 2, 'name':
'wangchuang'}
>>> dic['c'] = 33 #修改
>>> dic
{'a': 1, 'c': 33, 'b': 2, 'name':
'wangchuang'}
>>> dic.pop('c') #删除
33
>>> dic
{'a': 1, 'b': 2, 'name':
'wangchuang'}
del dic['name'] #删除
4.2字典详细用法
>>> dic.has_key('name') #有该键返回true,否则false
True
>>> dic.get('name') #get查看,就算没有这个键,也不会报错
'wangchuang'
>>> dic.get('name', 'wang') #如果没有,就返回‘wang‘,有就返回原来的值
'wangchuang'
>>> dic.items()
#将所有的字典项以列表方式返回
[('a', 1), ('b', 2), ('name',
'wangchuang')]
>>> dic.keys()
#显示keys
['a', 'b', 'name']
>>> dic.values() #显示值values
[1, 2, 'wangchuang']
>>> dic.setdefault('stu',1122) #如果字典里没有该键stu,就创建增加stu键,并把值1122赋给stu
1122
>>> dic
{'a': 1, 'stu': 1122, 'b': 2,
'name': 'wangchuang'}
>>>
dic.setdefault('stu',1133) #如果字典里有该键,就把值取出来
1122
>>> dic
{'a': 1, 'stu': 1122, 'b': 2,
'name': 'wangchuang'}
>>> cid =
{'name':'wc','add':'beijing'}
>>> dic.update(cid) #合并两个字典,如果键有重复的,cid的就生效
>>> dic
{'a': 1, 'stu': 1122, 'add':
'beijing', 'b': 2, 'name': 'wc'}
>>> name_info
{'age': '22', 'name': 'wang'}
>>> name_info2 = name_info
>>> name_info2
{'age': '22', 'name': 'wang'}
>>> name_info2['age'] =
44 #修改为44
>>> name_info
{'age': 44, 'name': 'wang'}
#以上name_info和name_info2某一个里面的元素,另一个也跟着变,类似于软链接
#下面使用copy就不会了,直接复制一份,对某一个操作不会影响另一个
>>> name_info3 =
name_info.copy()
>>> name_info3
{'age': 44, 'name': 'wang'}
>>> name_info3['age'] =
88 #修改为88
>>> name_info3
{'age': 88, 'name': 'wang'}
>>> name_info
{'age': 44, 'name': 'wang'} #值没有变
5、SET集合
特点:
- 无序
- 元素不重复
功能:
- 关系测试
- 去重
集合对象是一组无须排列的可哈希的值,集合成员可以做字典的键
x =
{1,2,3,4} #和字典一样,都是用{ }。Python2.6版本貌似不支持
5.1 更新集合
增:add update
>>> x.add(3) #再添加个重复的3
>>> x
set([1, 2, 3, 4]) #结果添加不上
>>> x.update('wang') #添加多个元素
>>> x
{'w', 1, 2, 3, 4, 'g', 'a', 'n'}
>>> x.add('chuang') #一次只能添加一个元素
>>> x
{'w', 1, 2, 3, 4, 'g', 'a', 'n',
'chuang'}
删:remove pop
>>>
x.pop() #删除
1
>>> x
set([2, 3, 4])
5.2
关系测试:交,并,差,对称差
>>> x =
{1,2,3,4}
>>> y =
{3,4,5,6}
>>> x &y #取交集
set([3, 4])
>>> x | y #取并集
set([1, 2, 3, 4, 5, 6])
>>> x - y
#求x与y之间的差集,我是x,y没有1,2
set([1, 2])
>>> y - x #求y与x之间的差集,我是y,x没有5,6
set([5, 6])
>>> x ^ y #对称差集
set([1, 2, 5, 6])
5.3
子集、超集
>>> x =
{1,2,3,4}
>>> z = {2,3,4}
>>>
z.issubset(x) #x包含z,所以z是x的子集
True
>>>
z.issuperset(x) #z是否包含x,x不是z的超集
False
python数据类型基本操作的更多相关文章
- PYTHON 100days学习笔记007-2:python数据类型补充(2)
目录 day007:python数据类型补充(2) 1.Python3 元组 1.1 访问元组 1.2 删除元组 1.3 元组运算符 1.4 元组索引,截取 1.5 元组内置函数 2.python3 ...
- PYTHON数据类型(进阶)
PYTHON数据类型(进阶) 一.字符串.列表.字典.元祖.集合的补充 str #captalize 首字母大写,其余小写 s1.capitalize() #swapcase 大小写翻转 s1.swa ...
- PYTHON数据类型(基础)
PYTHON数据类型(基础) 一.列表.字典.元祖.集合的基本操作 列表 创建 l1=[] l1=list() l1=list(['你好',6]) 增 l1.append('hu') l1.inser ...
- python 数据类型---布尔型& 字符串
python数据类型-----布尔型 真或假=>1或0 >>> 1==True True >>> 0==False True python 数据类型----- ...
- Python 数据类型及其用法
本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点型以及布尔类型.这些基本数据类型组 ...
- day01-day04总结- Python 数据类型及其用法
Python 数据类型及其用法: 本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点 ...
- Python数据类型及其方法详解
Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...
- Python学习笔记(五)--Python数据类型-数字及字符串
Python数据类型:123和'123'一样吗?>>> 123=='123'False>>> type(123)<type 'int'>>> ...
- python数据类型之元组、字典、集合
python数据类型元组.字典.集合 元组 python的元组与列表类似,不同的是元组是不可变的数据类型.元组使用小括号,列表使用方括号.当元组里只有一个元素是必须要加逗号: >>> ...
随机推荐
- Selenium-几种元素定位方式
#识别元素并操作#一般有如下几种方法,其中id最为常用.这里需要注意识别元素一定要用唯一id 1.find_element_by_id("value") #! /usr/bin/e ...
- 2018.5.8 Project review
1 .product introduced A. Function requirement (customer) The product function is control the 1KW and ...
- 【leetcode刷题笔记】ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- Arc066_E Addition and Subtraction Hard
传送门 题目大意 给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大. 题解 考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负, ...
- 使用Visual Studio进行单元测试-Shim类中无法找到参数包含CancellationTokenSource的方法
Shim类中无法找到参数包含CancellationTokenSource的方法,这句话有点绕口,看例子. 一.代码 public class CancellationDemo { public in ...
- Python 写文件时的Unicode设置
今天在把Evenote的笔记内容写为文件时出错: f.write(content) UnicodeEncodeError: 'gbk' codec can& ...
- Mybatis连接mysql数据库出现乱码
对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...
- lvs-nat搭建httpd
拓扑图: #172.16.252.10 [root@~ localhost]#route -n Kernel IP routing table Destination Gateway Genmask ...
- C/C++的区别
C和C++的关系:就像是win98跟winXP的关系.C++是在C的基础上增加了新的理论,玩出了新的花样.所以叫C加加. C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设 ...
- [hdu1712]ACboy needs your help分组背包
题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$ $for{\rm ...