Python数据类型内置函数

  - str(字符串)

  - list(列表)

  - tuple(元组)

  - dict(字典)

  - set(收集)


tuple(元组)的操作

  

- (count)统计元组中元素出现的次数,返回统计值

 # 统计元组中指定元素出现的次数,返回出现次数的值
tpe_1 = (2,3,4,2,5,6,2,7)
tpe_2 = tpe_1.count(2) print(tpe_2)
# 执行结果
3

- (index)指定元组的值找出它的索引,返回索引的值

 # 找出元组中指定的值的索引,返回索引
tpe_1 = (3,5,1,7,4,1)
tpe_2 = tpe_1.index(1)
tpe_3 = tpe_1.index(1,3,6) print(tpe_2)
# 执行结果
2 print(tpe_3)
# 执行结果
5

dict(字典)的操作

  

- (clear)对字典中的内容进行清除,返回None

 # 对字典中的内容进行清除,返回None
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.clear() print(dct_1)
# 执行结果
{} print(dct_2)
# 执行结果
None

- (copy)创建一块新的内存地址进行赋值,返回集合

 # 对字典进行拷贝,创建一个新的地址内存进行赋值
dct_1 = {'a':1,'b':2,'c':3} print(id(dct_1))
# 执行结果
3084069826424 dct_2 = dct_1.copy() print(id(dct_2))
# 执行结果
3084069854448 print(dct_2)
# 执行结果
{'a': 1, 'b': 2, 'c': 3}

- (get)指定字典的键,返回它的值,如果没有这个键返回None或你设置了它的值,则返回值

 # 指定字典的键,返回值
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.get('a')
dct_3 = dct_1.get('d',4)
dct_4 = dct_1.get('d') print(dct_2)
# 执行结果
1 print(dct_3)
# 执行结果
4 print(dct_4)
# 执行结果
None

- (items)使字典以元组的形式遍历

 # 将字典转化为元组形式遍历
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.items() print(dct_1)
# 执行结果
{'a': 1, 'b': 2, 'c': 3} print(dct_2)
# 执行结果
dict_items([('a', 1), ('b', 2), ('c', 3)]) # 直接循环打印出键和值
for k,v in dct_1.items():
print(k,v)
#执行结果
a 1
b 2
c 3 # 以元组形式打印出键和值
for i in dct_1.items():
print(i)
#执行结果
('a', 1)
('b', 2)
('c', 3)

- (keys)字典中所有的键返回

 # 字典中所有的键返回
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.keys() print(dct_2)
# 执行结果
dict_keys(['a', 'b', 'c'])

- (pop)删除一个键返回他值,如果没有这个键就要设置它的值,返回它的值,如果不设置报错

 # 删除一个键,返回这个键的值也可以字设置值,如果没有这个键必须设置值,如果没有,默认报错
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.pop('a') print(dct_2)
# 执行结果
1 print(dct_1)
# 执行结果
{'b': 2, 'c': 3} #dct_3 = dct_1.pop('a')
dct_4 = dct_1.pop('a',6) print(dct_3)
#执行结果
KeyError print(dct_4)
# 执行结果
6 dct_5 = dct_1.pop('d',5) print(dct_5)
# 执行结果
5

- (popitem)删除字典最后一对键值,返回删除的键值对以元组形式的返回

 # 删除字典中的键值,以元组形式返回键值
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.popitem() print(dct_2)
# 执行结果
('c', 3)

-  新增字典的键和值,返回值,如果只设置键没有设定值,则返回None

 # 在字典中新增的键值,返回值,如果没有设键,返回None
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.setdefault('d',4)
dct_3 = dct_1.setdefault('e') print(dct_1)
# 执行结果
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': None} print(dct_2)
# 执行结果
4 print(dct_3)
# 执行结果
None

- (update)修改字典中的键值,返回None

 # 修改字典中的键和值,返回None
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.update({'a':9,'b':6,'c':2,'d':4}) print(dct_1)
# 执行结果
{'a': 9, 'b': 6, 'c': 2, 'd': 4} print(dct_2)
# 执行结果
None

- (values)返回字典中所有的值

 # 返回字典中所有的值
dct_1 = {'a':1,'b':2,'c':3}
dct_2 = dct_1.values() print(dct_2)
# 执行结果
dict_values([1, 2, 3])

set(集合)的操作  

- (add)在集合末尾添加新的元素,返回None

 # 在集合内添加元素,如果添加重复则不显示
st_1 = {1,2,3,4,5}
st_2 = st_1.add(0)
st_3 = st_1.add(9) print(st)
# 执行结果
{0, 1, 2, 3, 4, 5, 9} print(st_2)
# 执行结果
None

-  (clear)集合内的元素全部清除,返回None

 # 清除集合内的元素,返回None
st_1 = {"one","strip",9}
st_2 = st_1.clear() print(st_1)
# 执行结果
set() print(st_2)
# 执行结果
None

- (copy)创建一个新的内存地址进行赋值,返回集合

 # 复制一个集合创意新的内存地址,返回集合
st_1 = {"practice","default"}
st_2 = st_1.copy() print(st_2)
# 执行结果
{'practice', 'default'}

- (difference)集合差集,返回集合一在集合二中不一致的元素

 # 集合中的差集,返回集合一存在集合二不存在的元素
st_1 = {"services","practice","strip","fast"}
st_2 = {"split","practice","services","over"}
st_3 = st_1.difference(st_2) print(st_1)
# 执行结果
{'services', 'fast', 'strip', 'practice'} print(st_3)
# 执行结果
{'fast', 'strip'}

  

- (difference_update)集合差集,删除集合一在集合二中不一致的元素,返回None

 # 集合中的差集,返回集合一在集合二中不一致的元素,返回None
st_1 = {"services","practice","strip","fast"}
st_2 = {"split","practice","services","over"}
st_3 = st_1.difference_update(st_2) print(st_1)
# 执行结果
{'fast', 'strip'} print(st_3)
# 执行结果
None

-  (discard)删除集合中指定的元素,返回None

 # 删除集合中指定的元素,返回None
st_1 = {28,36,21,"fast","split"}
st_2 = st_1.discard(21) print(st_1)
# 执行结果
{36, 'fast', 'split', 28} st_3 = st_1.discard("fast") print(st_1)
# 执行结果
{36, 'split', 28} print(st_2)
#执行结果
None

- (intersection)交叉,返回集合中共有的元素

 # 交叉,返回集合中共有的元素
st_1 = {"services","practice","strip","fast","over"}
st_2 = {"split","practice","services","over"}
st_3 = st_1.intersection(st_2) print(st_1)
# 执行结果
{'services', 'over', 'practice', 'fast', 'strip'} print(st_3)
# 执行结果
{'services', 'over', 'practice'}

- (intersection_update)交叉集合中共有的元素,返回None

 # 交叉集合中共有的元素,返回None
st_1 = {"services","practice","strip","fast","over"}
st_2 = {"split","practice","services","over"}
st_3 = st_1.intersection_update(st_2) print(st_1)
# 执行结果
{'services', 'over', 'practice'} print(st_3)
# 执行结果
None

- (isdisjoint)两个集合交叉,如果没有共同的元素返回Ture,有共同元素返回False

 # 两个集合具有空交集,没有返回Ture,有相同元素返回False
st_1 = {"services","practice","strip","fast","over"}
st_2 = {"split","practice","services","over"}
st_3 = st_1.isdisjoint(st_2) print(st_3)
# 执行结果
False st_4 = {99,"difference"}
st_5 = {"analysis","design","Implementation"}
st_6 = st_4.isdisjoint(st_5) print(st_6)
# 执行结果
Ture

- (issubset)判断集合一中的所有元素是否在指定集合内,返回布尔值

 #  判断集合1中所有元素是否包含在指定集合中,如果是返回Ture,否则返回False
st_1 = {1,2,3,4,5}
st_2 = {1,2,3,4,5,6,7,8}
st_3 = st_1.issubset(st_2) print(st_3)
# 执行结果
Ture st_4 = {1,2,3,4,5}
st_5 = {1,2,3,6,7,8}
st_6 = st_4.issubset(st_5)
print(st_6)
# 执行结果
False

- (issuperset)判断指定集合有没有在集合一中,返回布尔值

# 判断集合中指定的集合是否在集合1中,返回布尔值
st_1 = {1,2,3,4,5}
st_2 = {1,2,3}
st_3 = st_1.issuperset(st_2) print(st_3)
# 执行结果
Ture st_4 = {1,2,3,4,5}
st_5 = {1,2,3,6}
st_6 = st_4.issuperset(st_5) print(st_6)
# 执行结果
False

- (pop)随机删除指定集合内的元素,返回值

 # 随机删除集合中一个元素,返回删除元素
st_1 = {"super","subset","isdisjoint"}
st_2 = st_1.pop() print(st_1)
# 执行结果
{'subset', 'spuer'} print(st_2)
# 执行结果
isdisjoint

- (remove)删除集合内指定的成员,返回None

 # 指定删除一个集合中的成员,返回None
st_1 = {"super","subset","isdisjoint"}
st_2 = st_1.remove("isdisjoint") print(st_1)
# 执行结果
{'subset', 'super'} print(st_2)
# 执行结果
None

- (symmetric_difference)返回差集中不重复的元素

 # 指定集合,返回两个集合中不重复的元素,将重复的删除
st_1 = {"services","practice","strip","fast","over"}
st_2 = {"split","practice","services","over"}
st_3 = st_1.symmetric_difference(st_2) print(st_3)
# 执行结果
{'split', 'fast', 'strip'}

- (symmetric_difference)差集中不重复的元素,返回None

 # 两个集合中不重复的元素,返回None
st_1 = {"services","practice","strip","fast","over"}
st_2 = {"split","practice","services","over"}
st_3 = st_1.symmetric_difference_update(st_2) print(st_1)
# 执行结果
{'fast', 'strip', 'split'} print(st_3)
# 执行结果
None

- (union)合并两个集合将重复的元素删除,返回集合

 # 合并两个集合重复的删除,返回并集
st_1 = {"union","analysis","design"}
st_2 = {"cherry","union","design","analysis"}
st_3 = st_1.union(st_2) print(st_3)
# 执行结果
{'design', 'analysis', 'cherry', 'union'}

- (union_update)将两个集合重复的元素删除,返回None

 # 合并两个集合重复的删除,返回None
st_1 = {"union","analysis","design"}
st_2 = {"cherry","union","design","analysis"}
st_3 = st_1.update(st_2) print(st_1)
# 执行结果
{'design', 'analysis', 'cherry', 'union'} print(st_3)
# 执行结果
None

Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)的更多相关文章

  1. Python数据类型的内置函数之list(列表)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) list(列表)的操作 - (append)在列表最后追加指 ...

  2. Python数据类型的内置函数之str(字符串)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) str(字符串)的一些操作 - 字符串相连方法 # 字符串的 ...

  3. python数据类型常用内置函数之字符串

    1.strip, lstrip, rstrip x = ' jiahuifeng ' print(x.strip(' ')) print(x.lstrip(' ')) print(x.rstrip(' ...

  4. Python---基础---数据类型的内置函数

    2019-05-23 ---------------------------- 一. #数据类型的内置函数Python有哪些数据类型?Number   数值型string   字符型list     ...

  5. python 常见的内置函数

    内置函数 接下来,我们就一起来看看python里的内置函数.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数.这 ...

  6. python之路——内置函数和匿名函数

    阅读目录 楔子 内置函数 匿名函数 本章小结 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们 ...

  7. python学习交流 - 内置函数使用方法和应用举例

    内置函数 python提供了68个内置函数,在使用过程中用户不再需要定义函数来实现内置函数支持的功能.更重要的是内置函数的算法是经过python作者优化的,并且部分是使用c语言实现,通常来说使用内置函 ...

  8. 十五. Python基础(15)--内置函数-1

    十五. Python基础(15)--内置函数-1 1 ● eval(), exec(), compile() 执行字符串数据类型的python代码 检测#import os 'import' in c ...

  9. Python的常用内置函数介绍

    Python的常用内置函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.取绝对值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...

随机推荐

  1. Vue常见问题解决办法(一)ESLint检查报错

    vue.js报错“Do not use 'new' for side effects“(main.js里)解决办法 ESLint工具检查代码质量,main.js里的原代码是这样的: new Vue({ ...

  2. Shell脚本中的逻辑判断、文件目录属性判断、if的特殊用法、case判断

    1.Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi格式3:if …; then … ;elif …; then ...

  3. mysql执行sql脚本文件

    mysql执行sql脚本文件 方法一:使用cmd命令执行(windows下,unix或Linux在的其控制台下) [MySQL的bin目录]\mysql –u用户名 –p密码 –D数据库<[sq ...

  4. bootstrap4.0

    1.CDN库引用: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4 ...

  5. laravel seed填充数据步骤

  6. Python中serial的使用

    一.概述     pyserial模块封装了对串口的访问. 二.特性     在支持的平台上有统一的接口.     通过python属性访问串口设置.     支持不同的字节大小.停止位.校验位和流控 ...

  7. 在经过身份验证的服务中不支持跨域 javascript 回调

    在 asp.net web forms 站点中做了一个 wcf restful service 接口,开启了webforms 身份认证. 当 webforms 站点用户登录之后,访问 restful ...

  8. Linux----------mysql进阶

    目录 一.破解密码以及无密码登录 1.1 破解密码 1.2 无密码登录 1.3 定义不同的客户端 1.4 家目录下 二.视图 三.函数 3.1 系统函数 3.2 自定义函数 3.3 自定义函数中定义局 ...

  9. 深度系统 deepin 15.9 关闭桌面

    深度系统 deepin 15.9 关闭桌面 由于特别的原因,关闭深度的桌面. sudo systemctl disable lightdm 如果需要在命令模式进入桌面可以使用以下命令. sudo se ...

  10. 部署activiti 5.15.1的Activiti Explorer

    1.从官网下载activiti包,将其中的activiti-explorer.war文件拷贝到tomcat的webapps目录下: 2.将mysql驱动包文件mysql-connector-java- ...