Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)
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(集合)的更多相关文章
- Python数据类型的内置函数之list(列表)
Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) list(列表)的操作 - (append)在列表最后追加指 ...
- Python数据类型的内置函数之str(字符串)
Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) str(字符串)的一些操作 - 字符串相连方法 # 字符串的 ...
- python数据类型常用内置函数之字符串
1.strip, lstrip, rstrip x = ' jiahuifeng ' print(x.strip(' ')) print(x.lstrip(' ')) print(x.rstrip(' ...
- Python---基础---数据类型的内置函数
2019-05-23 ---------------------------- 一. #数据类型的内置函数Python有哪些数据类型?Number 数值型string 字符型list ...
- python 常见的内置函数
内置函数 接下来,我们就一起来看看python里的内置函数.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数.这 ...
- python之路——内置函数和匿名函数
阅读目录 楔子 内置函数 匿名函数 本章小结 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们 ...
- python学习交流 - 内置函数使用方法和应用举例
内置函数 python提供了68个内置函数,在使用过程中用户不再需要定义函数来实现内置函数支持的功能.更重要的是内置函数的算法是经过python作者优化的,并且部分是使用c语言实现,通常来说使用内置函 ...
- 十五. Python基础(15)--内置函数-1
十五. Python基础(15)--内置函数-1 1 ● eval(), exec(), compile() 执行字符串数据类型的python代码 检测#import os 'import' in c ...
- Python的常用内置函数介绍
Python的常用内置函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.取绝对值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...
随机推荐
- java效验只能为数字类型
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher /** * 利用正则表达式判断字符串是否是数字 * @param str * @ ...
- 再谈Retina下1px的解决方案
https://www.w3cplus.com/css/fix-1px-for-retina.html
- Mac os 下brew的安装与使用—— Homebrew
1.简介 brew 全称Homebrew 是Mac OSX上的软件包管理工具,相当于linux下的apt-get. 2.安装 2.1安装ruby工具 2.1.1 ruby简介 2.1.2 检查rub ...
- 17python-BS编程
1.前端概述(1)上网:就是下载网页(2)浏览器:就是一个解释器2.BS模式的了解(1)BS模式:-----b:browser(浏览器) s:server(服务端)(2)BS模式运行过程:brow ...
- Spring ConditionalOnProperty
Spring Annotation @ConditionalOnProperty spring doc解释 @Conditional: Indicates that a component is on ...
- nginx uwsgi django 配置
用django框架,uwsgi服务器作为动态服务器,nginx作为静态资源服务器 配置uWSGI,在项目目录下创建uwsgi.ini文件: [uwsgi] #使用nginx连接时使用 socket=1 ...
- Spring mvc后台重定向页面,实际前端不跳转
1.ajax不支持重定向 ajax是不支持重定向的,因为ajax本身就是局部刷新,不重新加载页面的. 2.若后台出现需要重定向页面,可以设置唯一错误码 前端ajax公共调用后,凡是遇到这一类错误码,则 ...
- vue2上传图片到OSS
第一步:安装阿里云OSS <!-- 引入在线资源 --> <script src="http://gosspublic.alicdn.com/aliyun-oss-sdk- ...
- AX3298添加新sensor
这是编译的工程目录. 1,先把sensor对应的驱动比如GC1034.c添加到工程.然后编译成库.会在res目录下生产sensor.bin文件 流程:编译后在debug目录生成 elf 文件AX329 ...
- Spark:DataFrame批量导入Hbase的两种方式(HFile、Hive)
Spark处理后的结果数据resultDataFrame可以有多种存储介质,比较常见是存储为文件.关系型数据库,非关系行数据库. 各种方式有各自的特点,对于海量数据而言,如果想要达到实时查询的目的,使 ...