python容器的內(nèi)置通用函數(shù)操作
學(xué)委之前分享了tuple/list和dict等類型,這幾個類型都是用來存放數(shù)據(jù)的容器。
python對它們有幾個通用的操作。
我們看一看。
這些數(shù)據(jù)容易的通用操作都有哪些?
除了數(shù)據(jù)的增刪查改(除了tuple不可變長度和元素不可變),我們還需要下面的操作:
- 比較比對操作
- 計算元素數(shù)量
- 把容器打印輸出
- 獲取容器類型
使用 == 操作符號比對是否相等
len(容器對象)
str(容器對象)
type(容器對象)#type支持對各種對象的類型進(jìn)行判斷
我們看看幾個容器的代碼
嚴(yán)格來說,我們不用tuple元組類型做數(shù)據(jù)容器。
我們更多用它來描述定長的結(jié)構(gòu)。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/8 12:40 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @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))
運行效果如下:

下面是list的同樣操作:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/8 12:40 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @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: 雷學(xué)委
# @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))
運行效果如下:

總結(jié)
上面的這些操作是python內(nèi)置函數(shù),對幾種數(shù)據(jù)容器,操作很對稱,也不用特別記憶。多敲幾次代碼就記住了。
到此這篇關(guān)于python容器的內(nèi)置通用函數(shù)操作的文章就介紹到這了,更多相關(guān)python內(nèi)置通用函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中對元組和列表按條件進(jìn)行排序的方法示例
這篇文章主要介紹了Python中對元組和列表按條件進(jìn)行排序的方法示例,需要的朋友可以參考下2015-11-11
Python函數(shù)實現(xiàn)學(xué)員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python函數(shù)實現(xiàn)學(xué)員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
Python 讀取.shp文件并生成圖幅編號的實現(xiàn)思路
這篇文章主要介紹了Python 讀取.shp文件并生成圖幅編號,代碼適用于需要處理和分析地理空間數(shù)據(jù)的場景,如城市規(guī)劃、環(huán)境監(jiān)測或自然資源管理,其中它可以幫助用戶讀取特定區(qū)域的Shapefile文件,確定其地理邊界,需要的朋友可以參考下2024-05-05

