温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python容器的内置通用函数怎么使用

发布时间:2021-11-23 17:34:02 来源:亿速云 阅读:126 作者:iii 栏目:开发技术

本篇内容主要讲解“python容器的内置通用函数怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python容器的内置通用函数怎么使用”吧!

这些数据容易的通用操作都有哪些?

除了数据的增删查改(除了tuple不可变长度和元素不可变),我们还需要下面的操作:

  • 比较比对操作

  • 计算元素数量

  • 把容器打印输出

  • 获取容器类型

使用 == 操作符号比对是否相等

len(容器对象)

str(容器对象)

type(容器对象)#type支持对各种对象的类型进行判断

我们看看几个容器的代码

严格来说,我们不用tuple元组类型做数据容器。

我们更多用它来描述定长的结构。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/8 12:40 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello


tuple1 = ("name", "leixuewei")
tuple2 = ("name", "leixuewei")
print("len : ", len(tuple1))
print("== : ", tuple1 == tuple2)
print("dict1 : ", str(tuple1))
print("type : ", type(tuple1))

运行效果如下:

python容器的内置通用函数怎么使用

下面是list的同样操作:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/8 12:40 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : commonlistops.py
# @Project : hello

list1 = ["name", "leixuewei"]
list2 = ["name", "leixuewei"]
print("len : ", len(list1))
print("== : ", list1 == list2)
print("list1 : ", str(list1))
print("type : ", type(list1))

下面是dict字典类型的操作:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/8 12:40 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello


dict1 = {"name": "leixuewei"}
dict2 = {"name": "leixuewei"}
print("len : ", len(dict1))
print("== : ", dict1 == dict2)
print("dict1 : ", str(dict1))
print("type : ", type(dict1))

到此,相信大家对“python容器的内置通用函数怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI